docs edit: eager loading using $includes in model
This commit is contained in:
parent
603e349de9
commit
b28c5eb19f
|
@ -406,6 +406,28 @@ ## Eager Loading
|
||||||
|
|
||||||
$books = Book::with(array('author', 'author.contacts'))->get();
|
$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>
|
<a name="constraining-eager-loads"></a>
|
||||||
## Constraining Eager Loads
|
## Constraining Eager Loads
|
||||||
|
|
||||||
|
@ -419,6 +441,7 @@ ## Constraining Eager Loads
|
||||||
|
|
||||||
In this example, we're eager loading the posts for the users, but only if the post's "title" column contains the word "first".
|
In this example, we're eager loading the posts for the users, but only if the post's "title" column contains the word "first".
|
||||||
|
|
||||||
|
|
||||||
<a name="getter-and-setter-methods"></a>
|
<a name="getter-and-setter-methods"></a>
|
||||||
## Getter & Setter Methods
|
## Getter & Setter Methods
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue