Files
eatvio-infra/app/Livewire/Recipe/Comment/Create.php

54 lines
1.1 KiB
PHP

<?php
namespace App\Livewire\Recipe\Comment;
use App\Models\Recipe;
use App\Models\RecipeComment;
use App\Models\User;
use Livewire\Component;
use Illuminate\View\View;
class Create extends Component
{
public Recipe $recipe;
public User $user;
public string $body = '';
public string|int $grade = 0;
public bool|string $isChecked = false;
public function render(): View
{
return view('livewire.recipe.comment.create');
}
public function toggle(): void
{
$this->isChecked = ! $this->isChecked;
}
public function save(): void
{
$this->validate([
'body' => 'required|min:3',
'grade' => 'required',
]);
$comment = new RecipeComment();
$comment->grade = $this->grade;
$comment->body = $this->body;
$comment->recipe_id = $this->recipe->id;
$comment->user_id = $this->user->id;
$comment->save();
$this->reset(['body', 'grade']);
$this->toggle();
$this->dispatch('commentAdded')->to('recipe.comment.show');
}
}