When do you use $this-> to access properties in Livewire? I've provided an example of a blade file from laravel.io.
<div class="thread-info-likes">
@if (Auth::guest())
<div class="text-gray-600 px-4 py-2 border-r inline-block">
<span class="text-2xl mr-1">?</span>
{{ count($this->reply->likes()) }}
</div>
@else
<button type="button" wire:click="toggleLike" class="text-lio-600 px-4 py-2 border-r">
<span class="text-2xl mr-1">?</span>
{{ count($this->reply->likes()) }}
</button>
@endif
</div>
final class LikeReply extends Component
{
use DispatchesJobs;
public $reply;
public function mount(Reply $reply): void
{
$this->reply = $reply;
}
public function toggleLike(): void
{
if (Auth::guest()) {
return;
}
if ($this->reply->isLikedBy(Auth::user())) {
$this->dispatchNow(new UnlikeReplyJob($this->reply, Auth::user()));
} else {
$this->dispatchNow(new LikeReplyJob($this->reply, Auth::user()));
}
}
}
Within my current project I'm doing something similar. Using dependency injection to update the Bill model being used before displaying the modal. But when I try to iterate through the hasMany relationship of files within blade, it only updates to show new Bill model files when I reference the Bill as $this->bill. I'm not sure if this is the correct way to handle this.
public function showFiles(Bill $bill) {
$this->bill = $bill;
$this->fileModal = true;
}
@foreach($this->bill->files as $file)
<div>
//showing file info
</div>
@endforeach