Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

I use exists() if I need to return a value, instead if I need to stop the execution of the script I use findOrFail(). You can use exists() for example when a user open a empty gallery and you want to print a response. You can use findOrFail() for example when a user open a gallery that does not exist and you need to return a 404.

0

As @lovePizza stated, using the below will stop execution and display an error page if it doesn't exist:

$q = Users::findOrFail(1);

If you don't want it to fail but attempt to run the query regardless then you can do this:

$q = Users::find(1);

if ($q) {
	echo 'exist';
} else {
	echo 'does not exist';
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

guidsen guidsen Joined 24 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.