public function removeFavoriteBy(Users $user) { if ($user->hasFavoredThis($this)) { Favorites::query()->where('user_id = :user:'******'user' => $user->id])->andWhere('favoritable_type = :type:', ['type' => get_class($this)])->andWhere('favoritable_id = :id:', ['id' => $this->id])->execute()->delete(); } return $this; }
public function favorites() { if (null == $this->favorites) { $this->favorites = Favorites::query()->where('user_id = :user:'******'user' => $this->id])->leftJoin('Movies', 'Movies.id = Favorites.favoritable_id AND favoritable_type = "Movies"')->orderBy('created_at DESC')->columns(['Favorites.id', 'Favorites.user_id', 'Favorites.favoritable_id AS movie_id', 'Favorites.created_at AS created_at', 'Movies.title AS movie_title'])->execute(); } return $this->favorites; }