php - 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;
谢谢!
解决方案
试试这个
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
在匿名函数中使用关键字的更多信息,请参见
推荐阅读
- inno-setup - Inno Setup:[Run] 部分中的错误处理
- java - 我们如何在 Spring Boot 中使用任何一个验证?
- r - R中的相图
- c - 按下按钮时的处理
- c# - Asp.net MVC 与 Okta 的集成 - 在 SigninManager.SignIn 之后替换问题 Okta 声明
- react-native - 使用 ARCGIS 反应本机
- javascript - 在节点反应js中获取'SyntaxError:Unexpected identifier'
- apache-kafka - ProcessorContext#header() 为空
- c# - 查找一个字符串到另一个字符串程序的排列
- docker-compose - Keycloak LetsEncrypt Nginx 反向代理 Docker Compose