[英]Laravel conditionally eager load, with or where query
I've got this query:我有这个查询:
$paginated = File::whereIn('model_id', $channelRules->pluck('id'))
->with('campaignChannelRule.channel.channel')
->where('model_type', 'campaign_channel_rule')
->orWhere(function($query) use($campaign) {
$query->where('model_type', 'campaign')->where('model_id', $campaign->id);
})
->paginate(15);
The problem is that when a file is of model_type
campaign
, I do not want to eager load ->with('campaignChannelRule.channel.channel')
.问题是,当文件是model_type
campaign
时,我不想急切加载->with('campaignChannelRule.channel.channel')
。
How can I achieve this?我怎样才能做到这一点?
Thankyou谢谢
I think it's because the wrong syntax of the query, try this one:我认为这是因为查询的语法错误,试试这个:
$paginated = File::with('campaignChannelRule.channel.channel')
->whereIn('model_id', $channelRules->pluck('id'))
->where(function ($w) use ($campaign) {
$w->where('model_type', 'campaign_channel_rule')
->orWhere(function ($query) use ($campaign) {
$query->where('model_type', 'campaign')->where('model_id', $campaign->id);
});
})
->paginate(15);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.