[英]Laravel eloquent returning wrong data
我有条件数据,它在返回数据时忽略了这些条件:
工作正常
$orders = Order::where('user_id', $user->id)->with(['customer', 'laundry', 'driver', 'driver.user', 'progresses' => function($p){
$p->orderby('created_at', 'asc');
}, 'progresses.progress', 'services'])->get();
返回错误数据
$orders = Order::where('user_id', $user->id)->with(['customer', 'laundry', 'driver', 'driver.user', 'progresses' => function($p){
$p->orderby('created_at', 'asc');
}, 'progresses.progress', 'services'])
->where('id', 'like', '%'.$this->search.'%')
->orWhere('transport', 'like', '%'.$this->search.'%')
->orWhere('amount', 'like', '%'.$this->search.'%')
->orWhere('weight', 'like', '%'.$this->search.'%')
->orWhere('total', 'like', '%'.$this->search.'%')
->paginate(10);
第二个查询返回所有orders
并忽略where('user_id', '=', $user->id)
。
where('user_id', '=', $user->id)
您应该在这里对您的搜索LIKE
查询进行分组
Order::with(
[
'customer',
'laundry',
'driver',
'driver.user',
'progresses' => function($p) {
$p->orderby('created_at', 'asc');
},
'progresses.progress',
'services'
]
)->where('user_id', $user->id)
->where(function($q) {
$q->where('id', 'like', '%'.$this->search.'%')
->orWhere('transport', 'like', '%'.$this->search.'%')
->orWhere('amount', 'like', '%'.$this->search.'%')
->orWhere('weight', 'like', '%'.$this->search.'%')
->orWhere('total', 'like', '%'.$this->search.'%');
})->paginate(10);
所以查询字符串看起来像:
WHERE user_id = ?
AND
(id LIKE ? OR transport LIKE ? OR amount LIKE ? OR weight LIKE ? OR total LIKE ?)
它将忽略where('user_id', '=', $user->id)
因为orWhere
在您的搜索LIKE
查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.