首页 > 解决方案 > Laravel Nested Eager Loading 如何使用 eloquent where 条件从内部与嵌套

问题描述

我正在寻找一种where在我的 laravel 雄辩关系中使用条件与急切加载的方法。

这是我的代码

  $data = Model::with(['stage_chatbot'=> function($stage_chatbot){
     $stage_chatbot->select('id', 'customer_id','stage','created_at');
     $stage_chatbot->orderBy('id', 'asc');
  }])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc');

  $data->where('stage_chatbot.stage', 'like', 'sampleValue')->get();
  return $data;

谢谢!

标签: phplaravellumen

解决方案


试试这个

use在 laravel 中使用关键字

$searchText = "sampleValue";
$data = Model::with(['stage_chatbot'=> function($stage_chatbot) use ($searchText) {
        $stage_chatbot->select('id', 'customer_id','stage','created_at')
        ->where('stage_chatbot.stage', 'like', "%".$searchText."%")
        ->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc')->get();

有关如何use在匿名函数中使用关键字的更多信息,请参见


推荐阅读