Ein appointment
viele Status haben können, aber die zuletzt erstellte Beziehung zwischen appointment
und status
ist der aktuelle Status des Termins. In meinem Termindetat möchte ich den zugehörigen Agenten, InstructionType
, SignUpCustomer
und den neuesten status
anzeigen. Ich möchte paginate
diese Datensätze bei 10
pro Seite.Wie kann ich diese eloquent Abfrage optimieren entfernen ähnliche Anfragen aus
Die entsprechenden Modelle sind:
Appointment
(belongsTo agent, instruction_type and sign_up_customer, belongsToMany status
)Agent
InstructionType
SignUpCustomer
ich diese Abfrage in meinem Controller verfügen, die ein Ergebnis erzeugt I Sende an Datatables.
Es produziert diese beiden Aussagen, die erste ist in Ordnung, ich brauche nicht die letzte. Wie kann ich die Abfrage optimieren, um die letzte Anweisung zu entfernen?
select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc limit 1
select `statuses`.*, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at`, `appointment_status`.`appointment_id` as `pivot_appointment_id`, `appointment_status`.`status_id` as `pivot_status_id`, `appointment_status`.`created_at` as `pivot_created_at`, `appointment_status`.`updated_at` as `pivot_updated_at` from `statuses` inner join `appointment_status` on `statuses`.`id` = `appointment_status`.`status_id` where `appointment_status`.`appointment_id` in ('2') order by `created_at` desc limit 1
Ich habe auch versucht, dies:
Modell:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function statuses()
{
return $this->belongsToMany(Status::class, 'appointment_status')->withTimestamps();
}
public function latestStatus()
{
return $this->statuses()->latest()->first();
}
Controller:
$query = Appointment::with(['agent', 'instruction_type', 'sign_up_customer', 'latestStatus']);
$table = Datatables::of($query);
Aber ich bekomme diese Fehlermeldung: BadMethodCallException in Builder.php Linie 2445: Aufruf zu undefinierter Methode Illuminate \ Database \ Query \ Builder :: addEagerConstraints()
Bitte beschreiben Sie, wie Ihre Tabellen aussehen, was Sie bekommen möchten und was Sie ausprobiert haben. https://StackOverflow.com/Help/how-to-ask – Indra
Das Entfernen der $ query-> paginate (10) reduziert die Abfrage auf 8. Allerdings habe ich immer noch die beiden Aussagen in den letzten Zeilen oben^ – showFocus
Wenn Sie ' Wenn Sie nach Leistung suchen, müssen Sie RAW-Anweisungen verwenden. Eloquent ist nicht auf Leistung ausgelegt. –