Laravel save many-to-many relationship in Eloquent mutators
eloquent, laravel, php
Solution
You can't attach many-to-many relations until you've saved the model. Call `save()` on the model before setting `$model->tags` and you should be OK. The reason for this is that the model needs to have an ID that Laravel can put in the pivot table, which needs the ID of both models.
Problem
I've got 2 models with a many-to-many relationship. I want to be able to set a specific attribute with an array of ids and make the relationship in the mutator like this: ``` <?php class Profile extends Eloquent { protected $fillable = [ 'name', 'photo', 'tags' ]; protected $appends = [ 'tags' ]; public function getTagsAttribute() { $tag_ids = []; $tags = $this->tags()->get([ 'tag_id' ]); foreach ($tags as $tag) { $tag_ids[] = $tag->tag_id; } return $tag_ids; } public function setTagsAttribute($tag_ids) { foreach ($tag_ids as $tag_id) { $this->tags()->attach($tag_id); } } public function tags() { return $this->belongsToMany('Tag'); } } <?php class Tag extends Eloquent { protected $fillable = [ 'title' ]; protected $appends = [ 'profiles' ]; public function getProfilesAttribute() { $profile_ids = []; $profiles = $this->profiles()->get([ 'profile_id' ]); foreach ($profiles as $profile) { $profile_ids[] = $profile->profile_id; } return $profile_ids; } public function profiles() { return $this->belongsToMany('Profile'); } } ``` However the `setTagsAttribute` function isn't working as expected. I'm getting the following error: `SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'profile_id' cannot be null (SQL: insert into`profile_tag`(`profile_id`,`tag_id`) values (?, ?)) (Bindings: array ( 0 => NULL, 1 => 1, ))`