public function postEditThemesSpeakers(Request $req) { $event_id = $req->id; $speakers = array_filter(explode(',', $req->speaker_ids)); $themes = array_filter(explode(',', $req->theme_ids)); foreach ($speakers as $user_id) { EventSpeaker::create(compact('event_id', 'user_id')); } foreach ($themes as $theme) { $theme_id = Theme::firstOrCreate(['name' => ['ilike', $theme]])->id; //TODO: usar ilike try { EventTheme::create(compact('event_id', 'theme_id')); } catch (QueryException $e) { if ($e->getCode() != EventTheme::ERR_UNIQUE_VIOLATION) { //the relation already existed, so it's fine throw $e; } } } return redirect(act('event@editThemesSpeakers', $req->id)); }