All the examples in your shared link are spot on.
I recall there's a different example in this talk.
Besides the use in your shared link, just like computed properties in Vue.js, I simply use computed properties in Livewire to write cleaner code getting info deriving from existing data.
Considering the below example:
public class PostFormModal extends Component
{
public Post $post;
public function boot()
{
$this->post = new Post();
}
public funtion setPost(Post $post): void
{
$this->post = $post;
}
#[Computed]
public function modalTitle(): string
{
if ($this->post->exists) {
return "Edit post #{$this->post->id}";
}
return "Create A New Post";
}
}
So the modal title will change depending on the binding post, or the current functionality.
A different example could be:
use Illuminate\Database\Eloquent\Collection;
public class PostList extends Component
{
public Collection $posts;
public function boot()
{
$this->posts = Post::all();
}
#[Computed]
public function numberOfPosts(): int
{
return $this->posts->count();
}
#[Computed]
public function archived(): int
{
return $this->posts->filter->archived;
}
}