Laravel eloquent 关系搜索无法正常工作

Laravel eloquent relation search not working properly

我是 laravel 的新人,面临 eloquent 搜索问题 query.I 有两个 table assetsassets_maintenance。他们与他们的关系是 assets hasMany assets_maintenance in asset Model assets belongsTo assets_maintenance in asset_maintenance model 当用户搜索时,我将以下代码用于获取结果。

$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);

但它没有给我正确的 results.Sometimes 它给了我另一项资产的记录或 inspector.I 想要的结果 inspector_id=?manitenance_due_date 大于 nowassets 处于活动状态表示未删除。

其他条件可选asset_reference,asset_detail,assets_category.name,maintenance_due_date,maintenance_cost

试试这个

$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);

这是对优先级的误解,在语言中 && / AND 总是在 || / OR 之前计算。这意味着例如您的查询的较短版本 inspector_id == x AND maintenance_due_date >= Now() AND maintenance_due_date like %text% 或 maintenance_cost 如 %text%can be true if only maintenance cost is liketext`。因为语句会像这样求值。

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

最后评估为真,或者在其他情况下,如果某些搜索为真,您可以找到未链接到特定检查器的元素。

将您的搜索逻辑包装在子 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%)