Ad

How To Add Where Clause On Implicit Model Binding?

- 1 answer

Currently, I have projects using Laravel 5.4 and I am trying to use model binding but I need another where clause, how can I do that?

I am using the implicit model binding on my controller, how can I provide another where clause on that? the where clause is belongs to User input (from The Request instance) which means it can be null

public function show(Request $request, User $user){

    $selfish = $request->input('selfish','no');
    $user->where('selfish',$selfish);

}

the response is empty or even error, I just wanna know how can I provide another where clause belongs to Request instance.

Ad

Answer

When doing a where() query you then need to use ->get() afterward to gather the information

public function show(Request $request, User $user){
    $selfish = $request->input('selfish','no');
    $user->where('selfish', $selfish)->get();

    return('example.view') //return of somekind you can also do a redirect()->back() if necessary;
}
Ad
source: stackoverflow.com
Ad