alexteie's avatar

livewire 4 mfc

Well please im little bit confused.

when i create an sfc component i wont get the data correct in the blade file? what do i wrong? $this->countTotal wont be displayed?

                <div class="widgets-icons bg-light-success text-success ms-auto">
                    <i class="fa-sharp fa-thin fa-people-group"></i>
                </div>
            </div>
            <div>
                <canvas id="contacten"></canvas>
            </div>
        </div>
    </div>
</div>
@endisland

<button wire:click="$refresh" wire:island="metrics" class="btn-sm btn-light">
    <i class="fa-sharp fa-thin fa-arrows-rotate"></i>
</button>
0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! Let’s clarify what’s happening with Livewire 4 SFC (Single File Components) and data visibility in Blade.

Key Points

  • SFC (Single File Component): This means your template (HTML), script (PHP), and style (CSS) all live in one file with a .php or .blade.php extension, sometimes using special syntax for Livewire.
  • Data Binding: Livewire binds public properties from your component class to the Blade template—use them in double curly braces like {{ $countTotal }}.

Why $this->countTotal Doesn't Show

  • In Blade Templates: You should not use $this->countTotal directly. Instead, reference it as {{ $countTotal }} in your Blade template section.
  • $this is only for the PHP (class) part.
  • Livewire automatically exposes public properties of the component for use in your Blade.

Example Solution

Assume your Livewire SFC looks like this:

<!-- app/Livewire/Metrics.php or .blade.php (Livewire 4 SFC) -->

<div>
    <div>
        <div class="widgets-icons bg-light-success text-success ms-auto">
            <i class="fa-sharp fa-thin fa-people-group"></i>
        </div>
    </div>
    <div>
        <!-- Display the value from your component -->
        <span>Total: {{ $countTotal }}</span>
        <canvas id="contacten"></canvas>
    </div>
</div>

<button wire:click="$refresh" wire:island="metrics" class="btn-sm btn-light">
    <i class="fa-sharp fa-thin fa-arrows-rotate"></i>
</button>
<?php

namespace App\Livewire;

use Livewire\Component;

class Metrics extends Component
{
    public int $countTotal = 0;

    public function mount()
    {
        // Calculate total (example)
        $this->countTotal = User::count(); // Or any logic
    }

    public function render()
    {
        return view('livewire.metrics'); // or just `return <<<'blade' ... blade;`
    }
}

In Blade (Template Section or Blade File)

Use:

{{ $countTotal }}

Not:

{{ $this->countTotal }}   <!-- This will not work! -->

Summary

  • Always print your public properties using double curly braces: {{ $countTotal }}
  • $this->countTotal is used in your PHP class, not in Blade/template.
  • If you just use {{ $countTotal }} in your Blade (or template section of SFC), it should display the value correctly.

If you share your SFC component code, I can help debug further!

tomcastillo's avatar

Hello @ geometry dash 3d, In Livewire 4 (SFC) you cannot use $this inside the Blade.

If your component has:

public int $countTotal = 0;

Then in the Blade you must use:

{{ $countTotal }}

❌ Not:

{{ $this->countTotal }}

Blade automatically receives public properties as variables.

Please or to participate in this conversation.