Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Laravel 5 Essentials

You're reading from   Laravel 5 Essentials Explore the fundamentals of Laravel, one of the most expressive and robust PHP frameworks available

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781785283017
Length 144 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Martin Bean Martin Bean
Author Profile Icon Martin Bean
Martin Bean
Arrow right icon
View More author details
Toc

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');
$averageTotal  = Order::avg('amount');
$lifetimeSales = Order::sum('amount');

Filtering records

Eloquent also ships with a feature-rich query builder that allows you to build queries in code, without having to write a single line...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image