를 제거하지만, appointment
및 status
의 경우 마지막으로 생성 된 관계는 약속의 현재 상태입니다. My Appointment Datatable에서 관련 에이전트 인 InstructionType
, SignUpCustomer
및 최신 status
을 표시하려고합니다. 나는 paginate
그 레코드를 10
에 기록하고 싶다.는 <code>appointment</code> 많은 <strong>상태</strong>을 가질 수 있습니다 유사한 쿼리
관련 모델은 다음과 같습니다
Appointment
(belongsTo agent, instruction_type and sign_up_customer, belongsToMany status
)Agent
InstructionType
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()
테이블 모양, 원하는 내용 및 시도한 것을 설명하십시오. https://stackoverflow.com/help/how-to-ask – Indra
$ query-> paginate (10)를 제거하면 쿼리가 8로 줄어 듭니다. 그러나^ – showFocus
위의 마지막 줄에 여전히 두 문장이 있습니다. RAW 문을 사용해야하는 성능을 찾고 있습니다. Eloquent는 성능을 위해 설계되지 않았습니다. –