Livewire

Building a Rating Component with Livewire v4

February 01, 2026 1 min read 10 views

Build an interactive rating component.

Component

@php
new class extends Livewire\Component {
    public int $rating = 0;
    public int $hoverRating = 0;
    public int $ratableId;
    public string $ratableType;
    
    public function setRating(int $value): void
    {
        $this->rating = $value;
        Rating::updateOrCreate(
            ['user_id' => auth()->id(), 'ratable_id' => $this->ratableId],
            ['value' => $value]
        );
    }
}
@endphp

@for($i = 1; $i <= 5; $i++) @endfor
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!