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 455<br />

.<br />

1 countries<br />

2 id - integer<br />

3 name - string<br />

4<br />

5 users<br />

6 id - integer<br />

7 country_id - integer<br />

8 name - string<br />

9<br />

10 posts<br />

11 id - integer<br />

12 user_id - integer<br />

13 title - string<br />

Though posts does not contain a country_id column, the hasManyThrough relation provides access<br />

to a country’s posts via $country->posts. To perform this query, Eloquent inspects the country_id<br />

on the intermediate users table. After finding the matching user IDs, they are used to query the<br />

posts table.<br />

Now that we have examined the table structure for the relationship, let’s define it on the Country<br />

model:<br />

.<br />

1

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

Saved successfully!

Ooh no, something went wrong!