如何优化此有说服力的查询以删除类似的查询

时间:2017-03-31 11:49:52

标签: mysql laravel eloquent laravel-eloquent

appointment可以包含多个状态,但appointmentstatus之间最后创建的关系是约会的当前状态。在我的Appointment Datatable中,我想显示相关的代理InstructionTypeSignUpCustomer和最新的status。我希望paginate每页10处记录这些记录。

相关模型是:

  1. AppointmentbelongsTo agent, instruction_type and sign_up_customer, belongsToMany status
  2. Agent
  3. InstructionType
  4. SignUpCustomer
  5. 我在我的控制器中有这个查询,它会产生我发送给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()

1 个答案:

答案 0 :(得分:0)

你能不能使用本地范围吗? https://laravel.com/docs/5.4/eloquent#local-scopes

public class SplashActivity extends AppCompatActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

我之前没有真正做到这一点,但我认为没有理由说它不起作用(我也不知道最终查询的优化程度如何)。