Reputation: 2415
I am confused about second and third arguments while defining eloquent relationships.
public function phone()
{
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
}
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
I understand the second argument in both relationships, it is foreign key which will be same. However, I am confused about third argument about what it refers to? I have an assumption that this is the key to which foreign key refers so it would be same too in both relationships. I do not know whether I am right or wrong. Please elaborate the answer.
Upvotes: 2
Views: 2864
Reputation: 152860
Your assumption is correct.
Both arguments refer to the exact same two columns.
Both foreign_key
parameters are the foreign key column on the phone
table.
(Without specifying anything that would be user_id
in your case)
local_key
and other_key
are the counterpart, the column the foreign key points to.
In most cases that's also the primary key column of the other table.
(Without specifying anything that would be id
in your case)
Upvotes: 3