nyaabooru/app/Livewire/Posts/View.php
2025-05-24 17:01:54 -04:00

36 lines
798 B
PHP

<?php
namespace App\Livewire\Posts;
use App\Models\Post;
use App\Models\Comment as PostComment;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Validate;
use Livewire\Component;
class View extends Component
{
public Post $post;
#[Validate('string|max:240')]
public string $message = '';
public function render()
{
return view('livewire.posts.view', [
'comments' => $this->post->comments->sortByDesc('created_at'),
])->title("Post {$this->post->id}");
}
public function postComment()
{
$this->validate();
$user = Auth::user();
if ($comment = PostComment::create(['message' => $this->message]))
{
$this->post->comments()->save($comment);
$user->comments()->save($comment);
}
return $this->redirect("/posts/{$this->post->id}");
}
}