Let’s talk a bit about scoped queries in Laravel. First here is a basic Book Eloquent Model with two handy scopes for type
and fewerPagesThan
.
//Book Model
class Book extends Eloquent {
public function scopeType( $query, $type ) {
return $query->where( 'books.type', '=', $type );
}
public function scopeFewerPagesThan( $query, $pages ) {
return $query->where( 'books.pages', '<=', $pages );
}
}
Using scoping
I often see confusion when we try to use scoping. Some would expect the following to give them back all 'autobiography' books:
Book::type('autobiography'); // This works right? Nope!
But it doesn't. Scoping functions will only refine your query. For instance all the following are great examples of using scoping:
Book::type('autobiography')->get();
Book::with('author')->type('autobiography')->get();
Book::fewerPagesThan(500)->type('autobiography')->get();
Building up scopes
When using scopes to refine and filter down which Models to retrieve, you will always need to add a ->get()
at the end. This allows you can chain multiple "scopes" onto each other as in the 3rd example. Basically it needs to know when you are done filtering down and when to actually hit the database.
So the 3rd example could be broken down like this to really drive home the point:
$maxPages = 500;
$books = new Book();
$books->type('autobiography');
if( $maxPages )
$books->fewerPagesThan($maxPages);
$opportunities->get();
In every case before the ->get()
call $books
var is a Eloquent/Builder
that is being built up.
After the ->get()
call $books
var gets overwritten with an Eloquent/Collection
that has been filled with our data from the database.
This is a very powerful and flexible to way to scope and filter down models that are in your database. It is better to filter them down with scopes in the DB query than filter down a collection that has already been loaded into memory. Make the your DB do some weight lifting for you. 😉