Removing likes from a post
This commit is contained in:
parent
0e7b23cb1e
commit
3aaf2788aa
@ -16,4 +16,10 @@ class LikeController extends Controller
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Post $post)
|
||||
{
|
||||
$request->user()->likes()->where("post_id", $post->id)->delete();
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
@ -35,4 +35,9 @@ class Post extends Model
|
||||
{
|
||||
return $this->hasMany(Like::class);
|
||||
}
|
||||
|
||||
public function checkLike(User $user)
|
||||
{
|
||||
return $this->likes->contains("user_id", $user->id);
|
||||
}
|
||||
}
|
||||
|
@ -50,4 +50,9 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasMany(Post::class);
|
||||
}
|
||||
|
||||
public function likes()
|
||||
{
|
||||
return $this->hasMany(Like::class);
|
||||
}
|
||||
}
|
||||
|
@ -11,18 +11,35 @@
|
||||
|
||||
<div class="p-3 flex items-center gap-4">
|
||||
@auth
|
||||
<form method="POST" action="{{ route('posts.likes.store', $post) }}">
|
||||
@csrf
|
||||
<div class="my-4">
|
||||
<button type="submit">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
|
||||
stroke="currentColor" class="size-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@if ($post->checkLike(auth()->user()))
|
||||
<form method="POST" action="{{ route('posts.likes.destroy', $post) }}">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<div class="my-4">
|
||||
<button type="submit">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="red" viewBox="0 0 24 24" stroke-width="1.5"
|
||||
stroke="currentColor" class="size-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
@else
|
||||
<form method="POST" action="{{ route('posts.likes.store', $post) }}">
|
||||
@csrf
|
||||
<div class="my-4">
|
||||
<button type="submit">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
|
||||
stroke-width="1.5" stroke="currentColor" class="size-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
@endif
|
||||
@endauth
|
||||
|
||||
<p>0 Likes</p>
|
||||
|
@ -33,3 +33,4 @@ Route::post("/images", [ImageController::class, "store"])->name("images.store");
|
||||
|
||||
// likes
|
||||
Route::post("/posts/{post}/likes", [LikeController::class, "store"])->name("posts.likes.store");
|
||||
Route::delete("/posts/{post}/likes", [LikeController::class, "destroy"])->name("posts.likes.destroy");
|
||||
|
Loading…
x
Reference in New Issue
Block a user