From b28c5eb19f2ec7941522b6289f02e543c0ed7d1b Mon Sep 17 00:00:00 2001 From: Steven Lischer Date: Mon, 2 Jul 2012 12:32:14 -0500 Subject: [PATCH 1/2] docs edit: eager loading using $includes in model --- laravel/documentation/database/eloquent.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index c2afc7a2..93f4896f 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -406,6 +406,28 @@ You may even eager load nested relationships. For example, let's assume our **Au $books = Book::with(array('author', 'author.contacts'))->get(); +If you find yourself eager loading the same models often, you may want to use **$includes** in the model. + + class Book extends Eloquent { + + public $includes = array('author'); + + public function author() + { + return $this->belongs_to('Author'); + } + + } + +**$includes** takes the same arguments that **with** takes. The following is now eagerly loaded. + + foreach (Book::all() as $book) + { + echo $book->author->name; + } + +> **Note:** Using **with** will override a models **$includes**. + ## Constraining Eager Loads @@ -419,6 +441,7 @@ Sometimes you may wish to eager load a relationship, but also specify a conditio In this example, we're eager loading the posts for the users, but only if the post's "title" column contains the word "first". + ## Getter & Setter Methods From 72558bda3b4c1b7a5848d43a466d7e3475a408d1 Mon Sep 17 00:00:00 2001 From: Steven Lischer Date: Mon, 2 Jul 2012 12:50:26 -0500 Subject: [PATCH 2/2] removed extra newline --- laravel/documentation/database/eloquent.md | 1 - 1 file changed, 1 deletion(-) diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index 93f4896f..17156c36 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -441,7 +441,6 @@ Sometimes you may wish to eager load a relationship, but also specify a conditio In this example, we're eager loading the posts for the users, but only if the post's "title" column contains the word "first". - ## Getter & Setter Methods