forked from eatvio/eatvio-infra
37 lines
710 B
PHP
37 lines
710 B
PHP
<?php
|
|
|
|
namespace App\Livewire\Diet;
|
|
|
|
use App\Models\Diet;
|
|
use Livewire\Component;
|
|
|
|
class Like extends Component
|
|
{
|
|
public Diet $diet;
|
|
public $isIconOnly = false;
|
|
|
|
public function mount(Diet $diet)
|
|
{
|
|
$this->diet = $diet;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.diet.like');
|
|
}
|
|
|
|
public function like(): void
|
|
{
|
|
if ($this->diet->isLiked()) {
|
|
$this->diet->removeLike();
|
|
} elseif (auth()->user()) {
|
|
$this->diet->likes()->create([
|
|
'user_id' => auth()->id(),
|
|
'diet_id' => $this->diet->id
|
|
]);
|
|
}
|
|
|
|
$this->dispatch('liked')->to('diet.like-count');
|
|
}
|
|
}
|