Browse Source

Merge pull request #895 from TomorrowToday/develop

Eloquent docs update: eager loading using $includes example
Taylor Otwell 12 years ago
parent
commit
06154abe8d
1 changed files with 22 additions and 0 deletions
  1. 22 0
      laravel/documentation/database/eloquent.md

+ 22 - 0
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**.
+
 <a name="constraining-eager-loads"></a>
 ## Constraining Eager Loads