How to check find method executed successfully in laravel

Laravel find method returns the model/models that have a primary key matching the given key or array of keys. We can check that the find method executed successfully or not in laravel. Because it returns the NULL value if the record does not exist with the specified key in the table, else it returns the eloquent collections.

Answers 1
  • Laravel find method on eloquent collection with if statement

    //Laravel find method with id
    $user = App\Models\User::find(1);
        echo "User exists";
    //Find method with an array of ids
    $userIds = [1,5,10];
    $users = App\Models\User::find($userIds);
        echo "Users exists";
  • Back to code snippet queries related laravel