$query = LeadsModel::with(
array(
'emails' => function ($q) use ($inputs)
{
$q->orwhere('email','like',"%john%");
$q->orwhere('email','like',"%mark%");
}
)
)
->whereHas
(
'emails', function ($q) use ($inputs)
{
$q->orwhere('email','like',"%john%");
$q->orwhere('email','like','%mark%');
}
);
$res = $query->get();
display_output($res);
return;
上面的代码只返回所有记录,并且没有条件应用于结果。我不确定是什么问题。我想要的是在查询中为与父项关联的子表添加条件,但是将返回应用条件的父数据和子数据
其他问题:
更新
这仍然记录所有数据,没有应用条件。
$query = LeadsModel::with(
array(
'emails' => function ($q) use ($inputs)
{
$q->orWhere('email','like',"%john%");
$q->orWhere('email','like',"%james%");
}
)
)
->whereHas
(
'emails', function ($q) use ($inputs)
{
$q->where('email','like',"%john%")->orWhere('email','like','%james%');
}
);
$res = $query->get();
display_output($res);
return;
在正常的sql中我猜它会是
从潜在客户中选择潜在客户。,lead_detail_emails。,其中lead_detail_emails leads.id = lead_detail_emails.lead_id和 (lead_detail_emails.email喜欢'%john%' OR lead_detail_emails.email喜欢'%james%');
答案 0 :(得分:4)
您需要嵌套在哪里:
whereHas('emails', function ($q)
{
$q->where(function ($q) {
$q->where('email','like',"%john%")
->orWhere('email','like','%mark%');
});
})
这是带有数据透视表(whereHas
)的belongsToMany
的示例查询:
select * from `table`
where `table`.`deleted_at` is null
and (select count(*) from `related_table` inner join `pivot_table` on `related_table`.`id` = `pivot_table`.`related_id`
where `pivot_table`.`user_id` = `table`.`id`
and (`some_field` like ? or `some_field` like ?)) >= 1