【发布时间】:2019-09-11 14:08:08
【问题描述】:
我已经用这些表创建了带有急切负载的 JOIN
- 交易(transaction_details的父)
- Transaction_details(Transaction 的child和Mutasi_logs的parent)
- Mutasi_logs(transaction_details 的子)
然后我添加一些过滤器,例如:
- 只显示我的数据(由登录用户创建的数据)
- 只显示我的萨尔多
- 只显示我的金额
我已经尝试过这段代码
blahlblah.....
->where('created_by',Auth::id())
->orWhere('amount','LIKE','%'.$search.'%')
->orWhere('saldo','LIKE','%'.$search.'%')
但在此处填写搜索字段时,条件无法正常工作(显示其他用户的数据,例如我的 saldo 和 amount )。
这是输出:
这里是我的完整代码:
MutasiLog::with([
'transaction_detail.transaction',
])
->orWhereHas('transaction_detail', function (&$q) use ($search) {
$q->where('id', 'like', '%' . $search . '%');
$q->orWhereHas('transaction',function($q) use ($search){
$q->where('description','LIKE','%'.$search.'%');
});
})
->where('created_by',Auth::id())
->orWhere('amount','LIKE','%'.$search.'%')
->orWhere('saldo','LIKE','%'.$search.'%')
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
【问题讨论】:
-
来自文档“您应该始终对 orWhere 调用进行分组,以避免在应用全局范围时出现意外行为。”。你可以找到一个例子there
-
已解决 - 谢谢 :D ...你能把你的评论回答一下吗?