seeking for a help. I have this code below but it cannot execute the query of search. I just need to view only the data of search based on the user id.
$id = Auth::id();
$promo = User::with('promos');
if($request->input('q')){
$promo->whereHas('promos', function ($query) use ($request) {
$query->where('title', 'LIKE', "%{$request->input('q')}%");
});
return view('admin.promo.index',[
'promos' => $promo->find($id)
]);
}
return view('admin.promo.index',[
'promos' => $promo->find($id)
]);
CodePudding user response:
You can do this from the Promo
side using whereHas
:
return view('admin.promo.index', [
'promos' => Promo::whereHas('user', fn ($q) => $q->whereKey(Auth::id()))
->when($request->input('q'), fn ($q, $input) => $q->where('title', 'LIKE', "%$input%"))
->get()
]);
You can use the when
method instead of using an if
statement.