2017-03-31 2 views
0

를 제거하지만, appointmentstatus의 경우 마지막으로 생성 된 관계는 약속의 현재 상태입니다. My Appointment Datatable에서 관련 에이전트 인 InstructionType, SignUpCustomer 및 최신 status을 표시하려고합니다. 나는 paginate 그 레코드를 10에 기록하고 싶다.는 <code>appointment</code> 많은 <strong>상태</strong>을 가질 수 있습니다 유사한 쿼리

관련 모델은 다음과 같습니다

  1. Appointment (belongsTo agent, instruction_type and sign_up_customer, belongsToMany status)
  2. Agent
  3. InstructionType
  4. SignUpCustomer

나는 결과를 생성 내 컨트롤러에서이 쿼리가 I Datatables에 보내십시오.

$query = Appointment::with(['agent', 'instruction_type', 'sign_up_customer']); 
      $query->with(['statuses' => function ($query) { 
       $query->latest()->first(); 
      }]); 
      $table = Datatables::of($query); 

이 두 문장을 생성합니다. 첫 번째 문장은 괜찮습니다. 마지막 문장은 필요하지 않습니다. 마지막 문장을 제거하기 위해 쿼리를 어떻게 최적화 할 수 있습니까?

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 

나는이 시도했다 :

모델 :

/** 
    * @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(); 
    } 

컨트롤러 :

$query = Appointment::with(['agent', 'instruction_type', 'sign_up_customer', 'latestStatus']); 
      $table = Datatables::of($query); 

하지만 난이 오류가 : Builder.php 라인 2445에 BadMethodCallException : 정의되지 않은 메서드 호출 Illuminate \ Database \ Query \ Builder :: addEagerConstraints()

+0

테이블 모양, 원하는 내용 및 시도한 것을 설명하십시오. https://stackoverflow.com/help/how-to-ask – Indra

+0

$ query-> paginate (10)를 제거하면 쿼리가 8로 줄어 듭니다. 그러나^ – showFocus

+0

위의 마지막 줄에 여전히 두 문장이 있습니다. RAW 문을 사용해야하는 성능을 찾고 있습니다. Eloquent는 성능을 위해 설계되지 않았습니다. –

답변

0

로컬 범위를 사용하여이 작업을 수행 할 수 없습니까? https://laravel.com/docs/5.4/eloquent#local-scopes

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

사실 전에 이런 짓을하지했지만, 나는 (나는 또한 결과 쿼리가 될 것이다 최적화하는 방법을 몰라) 작동하지 않을 이유가 없습니다.

+0

당신의 대답은 조금 부적절하다고 생각합니다. 질문은 SQL 쿼리를 최적화하는 방법에 관한 것입니다. 코드 표기가 없음 –

+0

의견을 보내 주셔서 감사합니다. – Joe

관련 문제