Laravel Eloquent: Retrieving Single Models / Aggregates

// Retrieve a model by its primary key...
$flight = App\Flight::find(1);

// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();
$flights = App\Flight::find([1, 2, 3]);

Not Found Exceptions

$model = App\Flight::findOrFail(1);

$model = App\Flight::where('legs', '>', 100)->firstOrFail();
Route::get('/api/flights/{id}', function ($id) {
    return App\Flight::findOrFail($id);
});

Retrieving Aggregates

$count = App\Flight::where('active', 1)->count();

$max = App\Flight::where('active', 1)->max('price');

Laravel Eloquent: Retrieving Single Models / Aggregates — Structure map

Clickable & Draggable!

Laravel Eloquent: Retrieving Single Models / Aggregates — Related pages: