我是laravel的新手,遇到了雄辩的搜索查询问题。我有两个表assets
和assets_maintenance
。它们与它们具有关系,在用户搜索时,资产模型assets
中的assets_maintenance
has assets
hasMany assets_maintenance
在资产维护模型中属于$data['reports'] = AssetMaintenance::with(['assets'])
->where('inspector_id',$tenant_inspector_id)
->where('maintenance_due_date','>=',Carbon::now()->startOfDay())
->whereHas('assets',function($query){
$query->active();//is_delete=0
})
->whereHas('assets',function($query) use($stext){
$query->where('asset_reference','like',"%{$stext}%")
->orWhere('asset_detail','like',"%{$stext}%");
})
->orWhereHas('assets.assetCategory',function($query) use($stext){
$query->where('assets_category.name','like',"%{$stext}%");
})
->orWhere('maintenance_due_date','like',"%{$stext}%")
->orWhere('maintenance_cost','like',"%{$stext}%")
->orderBy('maintenance_due_date','ASC')
// ->toSql();
->paginate(10);
,然后输入以下代码来获取结果。>
inspector_id=?
但是它没有给我正确的结果。有时它给了我另一项资产或检查员的记录。我想要的结果是manitenance_due_date
,now
比assets
和{{ 1}}有效表示未删除。
其他条件是可选的asset_reference
,asset_detail
,assets_category.name
,maintenance_due_date
,maintenance_cost
答案 0 :(得分:0)
尝试一下
$data['reports'] = AssetMaintenance::whereHas('assets',function($query) use($stext){
$query->active() //is_delete=0
$query->where('asset_reference','like',"%{$stext}%")
->orWhere('asset_detail','like',"%{$stext}%");
})
->where('inspector_id',$tenant_inspector_id)
->where('maintenance_due_date','>=',Carbon::now()->startOfDay())
->orWhereHas('assets.assetCategory',function($query) use($stext){
$query->where('assets_category.name','like',"%{$stext}%");
})
->orWhere('maintenance_due_date','like',"%{$stext}%")
->orWhere('maintenance_cost','like',"%{$stext}%")
->orderBy('maintenance_due_date','ASC')
// ->toSql();
->paginate(10);
答案 1 :(得分:0)
这是一种误解性的情况,在语言&& / AND
中,总是会在|| / OR
之前进行计算。举例来说,这意味着您的查询inspector_id == x AND maintenance_due_date >= Now() AND maintenance_due_date like %text%
或维护成本(例如%text%can be true if only maintenance cost is like
text`)的较短版本。因为该语句将像这样被评估。
inspector_id == x AND maintenance_due_date >= Now() AND maintenance_due_date like %text%` OR maintenance_cost like %text%
将相等的语句转换为其值
false AND false AND false OR true
由于AND的优先级较高,因此他们首先被评估。
false OR true
最后经过评估为true或在其他情况下,如果某些搜索为true,则可以找到未链接到特定检查器的元素。
将搜索逻辑包装在sub where子句中,将获得所需的结果。
$data['reports'] = AssetMaintenance::with(['assets'])
->where('inspector_id',$tenant_inspector_id)
->where('maintenance_due_date','>=',Carbon::now()->startOfDay())
->whereHas('assets',function($query){
$query->active();//is_delete=0
})
//here is the extra where
->where(function($query){
$query->whereHas('assets',function($query) use($stext){
$query->where('asset_reference','like',"%{$stext}%")
->orWhere('asset_detail','like',"%{$stext}%");
})
->orWhereHas('assets.assetCategory',function($query) use($stext){
$query->where('assets_category.name','like',"%{$stext}%");
})
->orWhere('maintenance_due_date','like',"%{$stext}%")
->orWhere('maintenance_cost','like',"%{$stext}%")
->orderBy('maintenance_due_date','ASC');
})
->paginate(10);
现在,这将包装与搜索相关的where语句,因此正确的优先级将在此处,再次是简短示例:inspector_id == x AND (maintenance_due_date like %text% OR maintenance_cost like %text%)