F
Filament10mo ago
Pints

Livewire Component passing variable to Blade Error

Hello! I have a livewire component called Announcement and this is the code <?php namespace App\Http\Livewire; use Livewire\Component; class Announcement extends Component { public $message = ''; public function render() { return view('livewire.announcement', [ 'message' => 'This is a test announcement!' ]); } } This is my blade file's code in /views/livewire/announcement.blade.php <div> <div> <h1>Announcement</h1> <h1>{{ $message }}</h1> </div> </div> I tried a lot of things of why the error message "Undefined variable $message" is always showing but I can't seem to find what solution is applicable.
No description
Solution:
I got it working bro, the solution is I deleted the Announcement Class and its View file and created a new livewire component.
Jump to solution
16 Replies
ericmp
ericmp10mo ago
try it like this:
public function render()
{
$this->message = 'This is a test announcement!';

return view('livewire.announcement');
}
public function render()
{
$this->message = 'This is a test announcement!';

return view('livewire.announcement');
}
Pints
PintsOP10mo ago
Still Undefined variable $message
ericmp
ericmp10mo ago
🤔
Pints
PintsOP10mo ago
I registered the livewire in the AppServiceProvider using this code FilamentView::registerRenderHook( PanelsRenderHook::BODY_START, fn (): View => view('livewire.announcement'), );
Vp
Vp10mo ago
Try like this
fn (): string => Blade::render('@livewire(\'announcement\')')
fn (): string => Blade::render('@livewire(\'announcement\')')
Pints
PintsOP10mo ago
This is now the error:
No description
Vp
Vp10mo ago
Just check out and I don't have a problem, what I did
// AppServiceProviders
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
FilamentView::registerRenderHook(
PanelsRenderHook::BODY_START,
fn (): string => Blade::render('@livewire(\'announcement\')'),
);
}
}

// Annoucement components
use Livewire\Component;

class Announcement extends Component
{
public $message = 'Test';

public function render()
{
return view('livewire.announcement');
}
}

// blade
<div>
{{ $message }}
</div>
// AppServiceProviders
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
FilamentView::registerRenderHook(
PanelsRenderHook::BODY_START,
fn (): string => Blade::render('@livewire(\'announcement\')'),
);
}
}

// Annoucement components
use Livewire\Component;

class Announcement extends Component
{
public $message = 'Test';

public function render()
{
return view('livewire.announcement');
}
}

// blade
<div>
{{ $message }}
</div>
Pints
PintsOP10mo ago
This is my code in AppServiceProvider <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Filament\Support\Facades\FilamentView; use Illuminate\Contracts\View\View; use Filament\View\PanelsRenderHook; use Illuminate\Support\Facades\Blade; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { } /** * Bootstrap any application services. */ public function boot(): void { FilamentView::registerRenderHook( PanelsRenderHook::GLOBAL_SEARCH_BEFORE, fn (): View => view('livewire.clock'), ); FilamentView::registerRenderHook( PanelsRenderHook::BODY_START, fn (): string => Blade::render('@livewire(\'announcement\')'), ); } }
Pints
PintsOP10mo ago
And this is the latest error code
No description
Vp
Vp10mo ago
what are the versions of filament and livewire?
Pints
PintsOP10mo ago
"filament/filament": "^3.2.71", "hasnayeen/themes": "^3.0", "jeffgreco13/filament-breezy": "^2.3", "laravel/framework": "^11.0", "laravel/tinker": "^2.9", "livewire/livewire": "^3.5",
ericmp
ericmp10mo ago
@Pints ^^
No description
Pints
PintsOP10mo ago
This is the current code,
No description
No description
No description
Vp
Vp10mo ago
no idea now
Pints
PintsOP10mo ago
It's okay bro, gonna find it out soon, thanks btw
Solution
Pints
Pints10mo ago
I got it working bro, the solution is I deleted the Announcement Class and its View file and created a new livewire component.

Did you find this page helpful?