Laravel Filament 3 Custom Page view props – Laravel

by
Ali Hasan
composer-php laravel laravel-filament

Quick Fix: Utilize Livewire’s $user property or directly access auth() data in the blade file without the need for a component.

The Solutions:

Solution 1: Livewire Data Passing

To pass data to the custom dashboard page blade view in Laravel Filament 3, you can utilize the Livewire component’s data-binding capabilities. Here’s an improved solution to achieve this:

  1. In your custom dashboard page class, define a public property to hold the user’s data:
  2. public $user;
    
  3. Within the mount() method of your page, fetch the authenticated user’s data and assign it to the $user property:
  4. public function mount()
    {
        $this->user = auth()->user();
    }
    
  5. Within the view() method, return the blade view along with the $user data:
  6. public static function view(): View
    {
        return view('filament.pages.dashboard', ['user' => auth()->user()]);
    }
    
  7. In the blade view, you can access the $user data as follows:
  8. @livewire.component('custom-dashboard-page')
    <x-filament-panels::page>
        Hello {{ $user->name }}!
    </x-filament-panels::page>
    @endcomponent
    

This approach ensures that the user’s data is properly passed to the blade view and can be accessed within the Livewire component.

Solution 2: Override getViewData function

Instead of overwriting the static view function, you can override the getViewData function from the BasePage class. This function is responsible for returning the data that is passed to the view. By overriding this function, you can return the data array that you want to pass to the view. Here’s an example:

<?php

namespace App\Filament\Pages;

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

use Filament\Pages\Page;
use Illuminate\Support\Facades\Auth;
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,
        ];
    }

    protected function getViewData(): array
    {
        $user = Auth::user(); // get the authenticated user

        return compact('user');
    }
}

With this change, the $user variable will be available in your view and you can use it as needed.

Q&A

How to pass user information and other datas to the blade page in Laravel Filament 3 custom dashboard page?

Overwrite the getViewData function from the BasePage class to return the data array.

In Laravel Filament 3, how to get authenticated user data in custom dashboard page view?

You can use auth()->user() to get the authenticated user data in the blade view.

Video Explanation:

The following video, titled "Laravel Filament Roles and Permissions Full Tutorial - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this video, we are going to work with Laravel Filament permissions. Laravel Filament. Laravel Spatie permission.