Как я могу реорганизовать что-то вроде этого Eloquent запроса? Я много читал о рефакторинге, но ничего не смог найти для этого.
public function trophy(Request $request)
{
// get trophy model
$trophy = Trophy::whereCommentID($request->comment_id)
->whereUserID(auth('api')->user()->id)->exists();
// create if not exists
if (!$trophy) {
$Trophy = Trophy::create([
'comment_id' => $request->comment_id,
'user_id' => auth('api')->user()->id,
'trophy_id' => $request->trophy
]);
return $Trophy;
}
// remove trophy if already exists with same trophy id
if ($trophy->trophy_id === $request->trophy_id) {
$trophy->delete();
return;
}
// change trophy id to new trophy id if not exists && was not same trophy id
$trophy->trophy_id = $request->trophy_id;
$trophy_id->save();
return $trophy_id;
}