_resourceId = $resourceId; $this->_resourceType = $resourceType; } /** * @return bool */ public function authorize() { $user = Auth::user(); return$user != null; } /** * @throws \Exception * @return CommandResponse */ public function execute() { $typeId = $this->_resourceType . '_id'; $existing = Favourite::where($typeId, '=', $this->_resourceId)->first(); $isFavourited = false; if ($existing) { $existing->delete(); } else { $fav = new Favourite(); $fav->$typeId = $this->_resourceId; $fav->user_id = Auth::user()->id; $fav->save(); $isFavourited = true; } return CommandResponse::succeed(['is_favourited' => $isFavourited]); } }