1
votes

I had a look at the eloquent relationship https://laravel.com/docs/5.2/eloquent-relationships#one-to-one.

My question is: Is there a difference between switching hasOne and belongsTo?

Laravel Docs:

public function phone()
{
    return $this->hasOne('App\Phone');
}

public function user()
{
    return $this->belongsTo('App\User');
}

can I swap the methods like:

public function phone()
{
    return $this->belongsTo('App\Phone');
}

public function user()
{
    return $this->hasOne('App\User');
}
1

1 Answers

1
votes

no you cannot simply swap, look at in which model foreign key resides, in your case phone is belongs to a user, so in your phone table the userid should be there, so your relationship must be like this

public function phone()
{
    return $this->belongsTo('App\User');
}

public function user()
{
    return $this->hasOne('App\Phone');
}