Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Cerebralj's avatar

Using $this-> within blade to access properties in Livewire?

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
0 likes
3 replies
LaryAI's avatar
Level 58

In Livewire, you can use $this-> to access public properties of the component class within the blade template. In the provided example, $this->reply is a public property of the LikeReply component class, which is being accessed within the blade template using $this->reply->likes(). Similarly, $this->bill is a public property of the component class being accessed within the blade template using $this->bill->files.

So, using $this-> to access public properties of the component class within the blade template is the correct way to handle this.

vincent15000's avatar

I don't agree Lary's answer.

According to me, you cannot use $this-> inside a blade component because it's in a view outside of an object context.

Snapey's avatar
Snapey
Best Answer
Level 122

within a blade template (only) you can use $this->bill and $bill they are the same thing

I never use $this in a blade file though as I then forget and try to use it in a regular blade file where it is NOT valid

edit: They are the same thing if you send 'bill' to the view in the render method, as you would do in a controller. If you dont pass data to the view then you must use $this

1 like

Please or to participate in this conversation.