Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Many to Many Polymorphic Relation with Extra Fields

How can I define a many to many polymorphic relation with extra fields?

I have three (or more, as it is a polymorphic relation) tables.

tags table: id, name

tagged table: id, tag_id, taggable_id, taggable_type, user_id

posts table: id, record, timestamps

users table: id, name, email

The user_id on the tagged table referes to the users table on column id. In my Post model I have:

public function tags()
{
    return $this->morphToMany('App\Tag', 'taggable','tagged');
}

and in my Tag model I have:

public function posts()
{
    return $this->morphedByMany('App\Post', 'taggable','tagged');
}

Then when I am try this in my controller:

$tag = new \App\Tag(
array(
    'tag'=>"someTag"
));
$tag->save()
$post = \App\Post::find($id);
$post->tags()->save($tag);

I get Integrity Constraint Violation for not having a user_id:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (hb.tagged, CONSTRAINT tagged_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id)) (SQL: insert into tagged (tag_id, taggable_id, taggable_type) values (26, 2, App\Resource)). Which is somewhat expected, as I have never had the chance to define or declare the user_id field.

Also, I have tried withPivot() on tags relation as follows, to no avail:

public function tags()
{
    return $this->morphToMany('App\Tag', 'taggable','tagged')->withPivot('user_id');
}
like image 818
CrackingTheCode Avatar asked May 28 '15 07:05

CrackingTheCode


People also ask

How to create many to many polymorphic relationship in Laravel?

In this tutorial, you will learn about laravel many to many polymorphic relationship and how to use create, and retrieve records from database tables using this relationship. Using “morphToMany ()” and “morphedByMany ()” eloquent method, you can create Many to Many Polymorphic Relationship in your laravel eloquent models.

What is a one of many relationship in Laravel?

A one-of-many polymorphic relationship A one-of-many relationship is a situation where one model can have multiple associations with more than one model, but you only want to retrieve one at a time. This takes advantage of Laravel’s ofMany helper methods along with morphOne to retrieve the desired single association.

What is many to many polymorphic relationship?

Many to many Polymorphic relationship is also a little bit complicated to understand. For example, if you have posts, videos, and tag tables, you require to connect with each other with your requirement like every post have multiple tags and same for videos too. Also every tag many are connected with multiple post or multiple videos too.

How do I find the parent of a polymorphic relationship?

Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the tags for a post, you may use the tags dynamic relationship property. You may retrieve the parent of a polymorphic relation from the polymorphic child model by accessing the name of the method.


1 Answers

Like in the comment: withPivot has nothing to do with saving/creating. If you want to pass additional pivot data when saving, do this:

$post->tags()->save($tag, ['user_id' => $userId]);
like image 75
Jarek Tkaczyk Avatar answered Sep 19 '22 14:09

Jarek Tkaczyk