在变量中使用where

I have this controller:

  public function category($name)
  {
        $users = User::all()->where('category','$name');
        return View('index',['users'=>$users]);  

 }

returning to the view:

You chose <mark> {{$users[0]->category}}</mark>

gives me error:

undefined offset:0

Oh boy. The problem is here '$name', change it to simple $name (without quotes).

If You want more explanations whats wrong with Your code (not related to this problem), just say it.

try this

Controller

public function category($name)
  {
        $user = User::where('category',$name)->select('category')->first();
        return View('index')->with('user', $user);  
 }

VIEW

You chose   <mark> {{ $user }}</mark>

Try this:

Controller :

public function category($name)
  {
        $users=User::where('category',$name)->get();
    return View('index',['users'=>$users]);
 }

View :

@foreach($users as $user) // to access all records
    {{ $user->category }}
@endforeach

{{$users[0]->category}} // to access first record