I am new in laravel and facing issue with eloquent search query.I have two table assets
and assets_maintenance
. They have relation with them which is assets
hasMany assets_maintenance
in asset Model assets
belongsTo assets_maintenance
in asset_maintenance model when user search then I put the following code to fetch results.
$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);
But its not giving me the correct results.Sometimes its giving me the record of another asset or inspector.I want result where inspector_id=?
, manitenance_due_date
is greater from now
and assets
is active means not deleted.
Other conditions are optional asset_reference
,asset_detail
,assets_category.name
, maintenance_due_date
, maintenance_cost