Rafat Rashid Rahi
Rafat Rashid Rahi

Reputation: 629

Laravel Filament 3 Custom Page view props

I have created a custom dashboard page and replaced it with main dashboard page. It loads perfectly. And the stats widget is also shown. But the problem is I can't pass props to the blade view. The static function view that I wrote doesn't actually exist in Page class. So it's not automatically executed. But protected static string $view = 'filament.pages.dashboard'; is executed and the view page is displayed. But I want to pass user information and other datas to the blade page. I am using filament 3. And this is my dashboard Page snippet

<?php

namespace App\Filament\Pages;

use App\Filament\Widgets\OverviewWidget;
use App\Filament\Widgets\StatsOverview;

use Filament\Pages\Page;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

class Dashboard extends Page
{
    protected static ?string $navigationIcon = 'heroicon-o-home';

    protected static string $view = 'filament.pages.dashboard';

    protected function getHeaderWidgets(): array
    {
        return [
            StatsOverview::class,
        ];
    }

    // create a view
    public static function view(): View
    {
        $user = auth()->user(); // get the authenticated user
        Log::inf("Hello Earth");
        return view('filament.pages.dashboard', compact('user'));
    }

}

And this is my view blade page

<x-filament-panels::page>
    Hello {{ $user->name }}
</x-filament-panels::page>

It says the $user is undefined

Upvotes: 1

Views: 5952

Answers (2)

adman_9000
adman_9000

Reputation: 49

Overwrite the getViewData function from the BasePage class so that instead of returning an empty array it returns the data array you want to pass to the view. You don't need to overwrite the static view function at all:

protected function getViewData(): array
    {

        $user = auth()->user(); // get the authenticated user
        return compact('user');
    }

Upvotes: 1

Vpa
Vpa

Reputation: 737

Actually it's part of livewire rather than filament.

You can pass data like below

public $user;

public function mount()
{
    $this->user = auth()->user();
}

Or If you want to display auth() data then you can simple display from blade file like auth()->user()->name without checking from component

Upvotes: 1

Related Questions