Check if record exist in collection Laravel

Viewed 4334

I have controller

public function users() {

 $users = User::all('id');

 return view('view', compact('users'))

}

Then in my view, i try to check if $users has id I need

@if (contains($users, Auth::user()->id)
   do something
@enif

but nothing happens. How can i check if collection has the id i need?

1 Answers

Try this:

if ($users->contains('id', Auth::user()->id)){}

You need to specify the field where to look as the first parameter.

Related