public function setSportsListAttribute($value) { $sportIds = []; $sports = explode(',', $value); foreach ($sports as $sportName) { /* firstOrCreate uses first instance or creates a new instantiation - stops sports table from duplicating sport names*/ $sport = Sport::firstOrCreate(array('sport' => $sportName)); $sportIds[] = $sport->id; if (!$sport->id) { dd($sport); } /* sync is a Laravel method to attach related models; sync accepts array of ids to be placed on pivot table only the ids in the array will be on the intermediate table sport_user pivot table*/ } $this->sports()->sync($sportIds); }