lundi 3 octobre 2016

Laravel 5.1 sync method with multiple attibutes as primary

I am using sync(on many to many) method in laravel 5.1 to add relational info to a user in DB.

My Table is

User-:

  • id(pk)
  • name
  • email

tags-:

  • id(pk)
  • name
  • slug

user_tags_mapping-:

  • user_id
  • tags_id
  • function
  • count

the relationship i have defined in between is a many to many

return $this->belongsToMany(
        tags::class, 'user_tags_mapping',
        'user_id', 'tags_id')->withTimestamps()
        ->withPivot('function', 'count');

Now i am using sync to insert value into this table like

$user->tags()->sync([tags_id => [function, count]]);

Now my problem is that when i use the same tags_id with different function the sync detaches the older value as the tags_is already exist.

Is There a way where i can use tags_id and function both as the parameters to sync to the table where function is a string value.

I have already defined the all the three parameter as primary inside the migration script, but it still didn't work.



via Chebli Mohamed

1 commentaire:

Lucy Carter a dit…
Ce commentaire a été supprimé par l'auteur.

Enregistrer un commentaire