Laravel嵌套的渴望加载如何使用雄辩的条件从内部嵌套 [英] Laravel Nested Eager Loading how to use eloquent where condition from inside with nested

查看:66
本文介绍了Laravel嵌套的渴望加载如何使用雄辩的条件从内部嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在我的幼虫雄辩的关系中使用where条件并渴望加载的方法.

I'm looking for a way to use where condition in my laravel eloquent relationship with eager loading.

这是我的代码

  $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;

谢谢!

推荐答案

您可以使用诸如此类的whereHas()雄辩方法.

You can use whereHas() eloquent method like this.

$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->whereHas('stage_chatbot', function ($stage_chatbot) {
   $stage_chatbot->where('stage', 'like', 'sampleValue')
})->get();
return $data;

这篇关于Laravel嵌套的渴望加载如何使用雄辩的条件从内部嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆