29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Eloquent: Relationships 461<br />

You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing<br />

the name of the method that performs the call to morphedByMany. In our case, that is the posts or<br />

videos methods on the Tag model. So, you will access those methods as dynamic properties:<br />

.<br />

1 $tag = App\Tag::find(1);<br />

2<br />

3 foreach ($tag->videos as $video) {<br />

4 //<br />

5 }<br />

Querying Relations<br />

Since all types of Eloquent relationships are defined via functions, you may call those functions to<br />

obtain an instance of the relationship without actually executing the relationship queries. In addition,<br />

all types of Eloquent relationships also serve as query builders, allowing you to continue to chain<br />

constraints onto the relationship query before finally executing the SQL against your database.<br />

For example, imagine a blog system in which a User model has many associated Post models:<br />

.<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!