Retrieving data

Eloquent provides you with numerous ways to fetch records from your database, each with their own appropriate use case. You can simply fetch all records in one go; a single record based on its primary key; records based on conditions; or a paginated list of either all or filtered records.

To fetch all records, we can use the aptly-named all method:

use App\Cat;
$cats = Cat::all();

To fetch a record by its primary key, you can use the find method:

$cat = Cat::find(1);

Along with the first and all methods, there are aggregate methods. These allow you to retrieve aggregate values (rather than a record set) from your database tables:

use App\Order; $orderCount = Order::count(); $maximumTotal = Order::max('amount'); $minimumTotal = Order::min('amount'); ...

Get Laravel 5 Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.