This repository was archived by the owner on Aug 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ public static function bootFavoriteable()
2525 */
2626 public function hasFavorite (): bool
2727 {
28- return (bool )$ this ->favorites ()->where ('user_id ' , auth ()->user ()->id )->first ();
28+ return (bool )$ this ->favorites ()->where ('user_id ' , auth ()->user ()->id )->count ();
2929 }
3030
3131 /**
@@ -35,7 +35,7 @@ public function hasFavorite(): bool
3535 */
3636 public function addFavorite ()
3737 {
38- if (!self :: hasFavorite ())
38+ if (!$ this -> hasFavorite ())
3939 return $ this ->favorites ()->create ();
4040 return false ;
4141 }
@@ -47,11 +47,21 @@ public function addFavorite()
4747 */
4848 public function unFavorite (): bool
4949 {
50- if (self :: hasFavorite ())
50+ if ($ this -> hasFavorite ())
5151 return $ this ->favorites ()->where ('user_id ' , auth ()->user ()->id )->delete ();
5252 return false ;
5353 }
5454
55+ /**
56+ * @example $post->toggleFavorite()
57+ *
58+ * @return mixed
59+ */
60+ public function toggleFavorite ()
61+ {
62+ return $ this ->hasFavorite () ? $ this ->unFavorite () : $ this ->addFavorite ();
63+ }
64+
5565 /**
5666 * @example $post->deleteFavorites()
5767 *
Original file line number Diff line number Diff line change @@ -18,9 +18,9 @@ trait UserFavorites
1818 */
1919 public function favorites ()
2020 {
21- if (self :: hasFavorite ()) {
21+ if ($ this -> hasFavorite ()) {
2222 $ collection = new Collection ;
23- foreach ($ this ->userFavorites ()-> get () as $ favorite )
23+ foreach ($ this ->userFavorites as $ favorite )
2424 $ collection ->push (app ($ favorite ->favoriteable_type )->find ($ favorite ->favoriteable_id ));
2525
2626 return $ collection ;
@@ -36,7 +36,7 @@ public function favorites()
3636 */
3737 public function hasFavorite (): bool
3838 {
39- return ( bool ) $ this ->userFavorites ()-> count ();
39+ return ! $ this ->userFavorites -> isEmpty ();
4040 }
4141
4242 /**
You can’t perform that action at this time.
0 commit comments