BelongsTo与HasOne相反.
We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with
User
andPhone
models.
I'm giving hasOne relation from User to Phone.
class User extends Model
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
Using this relation, I'm able to get Phone model data using User model.
But it is not possible with Inverse process using HasOne. Like Access User model using Phone model.
如果我想使用手机访问用户模型,那么有必要在手机模型中添加BelongsTo.
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
You can refer this link for more detail.