I'm working with Laravel v10 and in my project I tried the following to make a custom component with livewire.
I ran: php artisan make:component CounterDisplay
created a new file named: resources/views/components/counter-display.blade.php which goes like this:
<div>
Counter Value: {{ $count }}
</div>
Then ran php artisan make:livewire Counter to created a new livewire component (app/Http/Livewire/Counter.php):
// app/Livewire/Counter.php
namespace App\Livewire;
use Livewire\Component;
class Counter extends Component
{
public $count = 0;
public function increment()
{
$this->count++;
}
public function render()
{
return view('livewire.counter');
}
}
Then created a new blade file resources/views/livewire/counter.blade.php:
<!-- resources/views/livewire/counter.blade.php -->
<div>
<button wire:click="increment">Increment</button>
<x-counter-display :count="$count" />
</div>
With this route:
use App\Livewire\Counter;
Route::get('/counter', function () {
return view('counter', ['counter' => Counter::class]);
});
And also created a blade file named counter.blade.php:
<!-- resources/views/counter.blade.php -->
@extends('layouts.app')
@section('content')
<livewire:counter />
@endsection
Now when I visit the /counter, nothing show up!
However when I check dd at counter-display.blade.php,:
@dd(2) <!-- Properly returns 2 but the html not show up -->
<div>
Counter Value: {{ $count }}
</div>
It will show the output but strange that the even the Counter Value: text is not printed while there is no error.
So if you have any idea what's going wrong about this, please let me know...
I'm really stuck with this!
You would call the livewire component on your blade like