Laravel - Getting Started - Retrieving Single Models

In addition to retrieving all of the records matching a given query, you may also retrieve single records using the find, first, or firstWhere methods. Instead of returning a collection of models, these methods return a single model instance:

    
    use App\Models\Flight;
    
    // Retrieve a model by its primary key...
    $flight = Flight::find(1);
    
    // Retrieve the first model matching the query constraints...
    $flight = Flight::where('active', 1)->first();
    
    // Alternative to retrieving the first model matching the query constraints...
    $flight = Flight::firstWhere('active', 1);
	

Sometimes you may wish to retrieve the first result of a query or perform some other action if no results are found. The firstOr method will return the first result matching the query or, if no results are found, execute the given closure. The value returned by the closure will be considered the result of the firstOr method:

    
    $model = Flight::where('legs', '>', 3)->firstOr(function () {
        // ...
    });