Laravel check collection if empty

Edward Ziadeh • 27 February 2019
Blog in group Laravel
0 comments
0 likes

When you have a Collection let's say:

$posts = Post::where('user_id', 12)->get();

and you want to check if it is not empty do something.

$posts = Post::where('user_id', 12)->get();
if(!$posts->isEmpty()){
  dd('true');
}else{
  dd('false');
}

also you can use:

if(!$posts->isEmpty())

if($posts->count() > 0)

if($posts->first())

if(count($posts->count()))