29.07.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 447<br />

column of the Phone record. If you would like the relationship to use a value other than id, you may<br />

pass a third argument to the hasOne method specifying your custom key:<br />

.<br />

1 return $this->hasOne('App\Phone', 'foreign_key', 'local_key');<br />

Defining The Inverse Of The Relation<br />

So, we can access the Phone model from our User. Now, let’s define a relationship on the Phone<br />

model that will let us access the User that owns the phone. We can define the inverse of a hasOne<br />

relationship using the belongsTo method:<br />

.<br />

1

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

Saved successfully!

Ooh no, something went wrong!