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.

2 Answers

0
CodeBoyCode On Best Solutions

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;
}
0
Surender Singh Rawat On
public function show(Request $request, User $user){
    //validate all inputs first

    foreach($request->all() as $key => $value){ //retrive all inputs

       if(isset($request->filled($key)))  //check input 
          $user = $user->where($key, $request->{$key}); // add where clause 

    }       

    return $user->get();   //get users which satisfy where clause      

}