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.
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';
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community