-
app/Http/Livewire/LikeButton.php
Open in GitHubuse App\Models\Like; use App\Models\Post; use Livewire\Component; class LikeButton extends Component { public $post; public $user; final public function mount($post): void { $this->user = \auth()->user(); $this->post = Post::findOrFail($post); } final public function store(): void { if ($this->user->id === $this->post->user_id) { $this->dispatchBrowserEvent('error', ['type' => 'error', 'message' => 'You Cannot Like Your Own Post!']); return; } $exist = Like::where('user_id', '=', $this->user->id)->where('post_id', '=', $this->post->id)->first(); if ($exist) { $this->dispatchBrowserEvent('error', ['type' => 'error', 'message' => 'You Have Already Liked Or Disliked This Post!']); return; } $new = new Like(); $new->user_id = $this->user->id; $new->post_id = $this->post->id; $new->like = 1; $new->save(); $this->dispatchBrowserEvent('success', ['type' => 'success', 'message' => 'Your Like Was Successfully Applied!']); } final public function render(): \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View | \Illuminate\Contracts\Foundation\Application { return \view('livewire.like-button'); } }
-
resources/views/livewire/like-button.blade.php
Open in GitHub<div style="display: inline;"> <a wire:click="store({{ $post->id }})" class="text-green" data-toggle="tooltip" style="margin-right: 16px;" data-original-title="@lang('forum.like-post')"> <i class="icon-like {{ config('other.font-awesome') }} fa-thumbs-up fa-2x @if(auth()->user()->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first()) fa-beat @endif"></i> <span class="count" style="font-size: 20px;">{{ $post->likes()->count() }}</span> </a> </div>
-
resources/views/forum/topic.blade.php
Open in GitHub// ... <div class="likes"> <span class="badge-extra"> @livewire('like-button', ['post' => $p->id]) @livewire('dislike-button', ['post' => $p->id]) </span> </div> // ...