Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] Saving morphTo relation. #11918

Closed
SachinAgarwal1337 opened this issue Jan 16, 2016 · 2 comments
Closed

[5.1] Saving morphTo relation. #11918

SachinAgarwal1337 opened this issue Jan 16, 2016 · 2 comments

Comments

@SachinAgarwal1337
Copy link

When I try to perform save() or create() on morphTo relation of my User model. It always gives this error:

Call to undefined method Illuminate\Database\Query\Builder::create()

Where as if I do the reverse, i.e., If I perform save() or create() from morphOne relation of my Admin model. It works perfectly.

Is this a bug? or is it suppose to be like this?

// User model:
 public function profile()
 {
    return $this->morphTo('profile', 'profile_type', 'profile_id');
 }

// Admin model:
public function user()
{
    return $this->morphOne(User::class, 'profile', 'profile_type', 'profile_id', 'id');
}

@patrickcarlohickman
Copy link
Contributor

It's supposed to be like this. morphTo relationships are treated as the belongsTo side of the relationship. morphOne/morphMany relationships are treated as the hasOne/hasMany side of the relationship.

The belongsTo side of the relationship does not have the save()/create() methods. It has the associate() method. Only the hasOne/hasMany side of the relationship has the save/create methods.

@SachinAgarwal1337
Copy link
Author

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants