2017-03-31 1 views
0

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:

  1. Appointment (belongsTo agent, instruction_type and sign_up_customer, belongsToMany status)
  2. Agent
  3. InstructionType
  4. 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()

+0

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

+0

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

+0

Wenn Sie ' Wenn Sie nach Leistung suchen, müssen Sie RAW-Anweisungen verwenden. Eloquent ist nicht auf Leistung ausgelegt. –

Antwort

0

Können Sie dies nicht mit einem lokalen Bereich tun? https://laravel.com/docs/5.4/eloquent#local-scopes

public function scopeLatest($query) 
{ 
    return $query->orderBy('created_at', 'DESC')->offset(0)->limit(1); 
} 

Ich habe eigentlich nicht zum ersten Mal, aber ich sehe keinen Grund, warum es nicht funktionieren würde (ich habe auch keine Ahnung, optimiert, wie die resultierende Abfrage wäre).

+0

Ich denke, deine Antwort ist ein wenig irrelevant. Die Frage ist, wie man die SQL-Abfrage optimiert. Nicht die Code-Darstellung –

+0

Danke für Ihre Meinung. – Joe

Verwandte Themen