Laravel Check If Query Has Results or Not

Hello devs,

In this laravel collection check if value exists tutorial, I will show you how we can check if collection property exists. To check if record exists in collection in laravel, laravel provides exists() eloquent method.

Using this exists() method, we can easily check if collection exists laravel. If you don't know laravel check if collection is empty, then this tutorial is for you. I am here to give you the complete solution of laravel check if key exists in collection.

Example code:

$post = Post::where('user_id',auth()->id())->exists(); // return true or false

if($post){
   //collection exists
}

return redirect()->back()->withError('No post found.');

 

Read also: Way to Check Laravel Application Running Environment

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js