06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Eloquent: Relationships 578<br />

1 /**<br />

2 * Get the user that owns the phone.<br />

3 */<br />

4 public function user()<br />

5 {<br />

6 return $this->belongsTo('App\User', 'foreign_key');<br />

7 }<br />

If your parent model does not use id as its primary key, or you wish to join the child model to a<br />

different column, you may pass a third argument to the belongsTo method specifying your parent<br />

table’s custom key:<br />

1 /**<br />

2 * Get the user that owns the phone.<br />

3 */<br />

4 public function user()<br />

5 {<br />

6 return $this->belongsTo('App\User', 'foreign_key', 'other_key');<br />

7 }<br />

One To Many<br />

A “one-to-many” relationship is used to define relationships where a single model owns any amount<br />

of other models. For example, a blog post may have an infinite number of comments. Like all<br />

other Eloquent relationships, one-to-many relationships are defined by placing a function on your<br />

Eloquent model:<br />

1

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

Saved successfully!

Ooh no, something went wrong!