public static function add($topic, $user) { try { return static::create(['topic_id' => $topic->topic_id, 'user_id' => $user->user_id]); } catch (QueryException $ex) { // Do nothing if already watching. Rethrow everything else. if (!is_sql_unique_exception($ex)) { throw $ex; } } }
public function profileCustomization() { if ($this->profileCustomization === null) { try { $this->profileCustomization = $this->userProfileCustomization()->firstOrCreate([]); } catch (Exception $ex) { if (is_sql_unique_exception($ex)) { // retry on duplicate return $this->profileCustomization(); } throw $ex; } } return $this->profileCustomization; }
public function favourite($user) { DB::transaction(function () use($user) { try { FavouriteBeatmapset::create(['user_id' => $user->user_id, 'beatmapset_id' => $this->beatmapset_id]); } catch (QueryException $e) { if (is_sql_unique_exception($e)) { return; } else { throw $e; } } $this->favourite_count = DB::raw('favourite_count + 1'); $this->save(); }); }