Jelajahi Sumber

Update LikeService, fix likedBy method

Daniel Supernault 4 tahun lalu
induk
melakukan
a5e64da69b
1 mengubah file dengan 19 tambahan dan 5 penghapusan
  1. 19 5
      app/Services/LikeService.php

+ 19 - 5
app/Services/LikeService.php

@@ -50,13 +50,27 @@ class LikeService {
 
 	public static function likedBy($status)
 	{
+		$empty = [
+			'username' => null,
+			'others' => false
+		];
+
+		if(!$status) {
+			return $empty;
+		}
+
 		if(!$status->likes_count) {
-			return [
-				'username' => null,
-				'others' => false
-			];
+			return $empty;
 		}
-		$id = Like::whereStatusId($status->id)->first()->profile_id;
+
+		$like = Like::whereStatusId($status->id)->first();
+
+		if(!$like) {
+			return $empty;
+		}
+
+		$id = $like->profile_id;
+
 		return [
 			'username' => ProfileService::get($id)['username'],
 			'others' => $status->likes_count >= 5,