display information about the logged in user at the user resource level

good evening sir @tuto1902 , I have a php filament project, which has 2 dashboards, one "admin" and the other "user". I created a user resource(list all registered users), which is displayed in the 2 dashboards, but I would like that at the level of the user dashboard("user"), I would like that only the information of the connected user is displayed, and can only modify his information and not for others. thank you for helping me.
Solution:
the UserPanelProvider class should be automatically created when you create a new panel using php artisan make:filament-panel User and it should be located in app\Providers\Filament\UserPanelProvider.php It also should extend Filament\PanelProvider, not FilamentServiceProvider. And that error means that Laravel can't find the class you are extending from. Which usually means you either using the wron class name, or you didn't import the class with use...
Jump to solution
12 Replies
tuto1902
tuto1902•14mo ago
Of course. Pretty simple. Here are the concepts you need to know - Model Global Scopes - Policies - Middleware Classes Model Global Scopes basically apply specific filters to every single query you perform on a model, like for example, filtering the users by the currenlty logged in user id Policies apply rules to what operations can be performed on a model. This is just a class that is then bound to a model. It has several predefined methods that return true (the action can be performed) or false (not allowed to do that). These are normal operations like create, delete or update. The good news is that filament will abide by any existing policies. So, if you are not allowed to modify a record for example, filament will not show the edit action button or allow you to access the edit page. Middleware classes are just things that get executed before every request. They can be added to you filament panel configuration and can be used to apply global scopes in your panel. Here are some examples from my Sunday livestream.
// Global scope to prevent doctors (users) from seeing other doctor's schedules
Schedule::addGlobalScope(function (Builder $query) {
$query->whereBelongsTo(Filament::auth()->user());
});
// Global scope to prevent doctors (users) from seeing other doctor's schedules
Schedule::addGlobalScope(function (Builder $query) {
$query->whereBelongsTo(Filament::auth()->user());
});
// Middleware class that applies this global scope on each request.
class AssignGlobalScopes
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
Schedule::addGlobalScope(function (Builder $query) {
$query->whereBelongsTo(Filament::auth()->user(), 'owner');
});
return $next($request);
}
}
// Middleware class that applies this global scope on each request.
class AssignGlobalScopes
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
Schedule::addGlobalScope(function (Builder $query) {
$query->whereBelongsTo(Filament::auth()->user(), 'owner');
});
return $next($request);
}
}
// Policy that only allows admins to update users
namespace App\Policies;

class UserPolicy
{
public function update(): bool
{
return auth()->user()->role->name == 'admin';
}
}

// These are registered in the AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
User::class => UserPolicy::class
];
}
// Policy that only allows admins to update users
namespace App\Policies;

class UserPolicy
{
public function update(): bool
{
return auth()->user()->role->name == 'admin';
}
}

// These are registered in the AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
User::class => UserPolicy::class
];
}
// Middleware applied to your panel configuration
class UserPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->middleware([
// ...
AssignGlobalScopes::class
])
}
}
// Middleware applied to your panel configuration
class UserPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->middleware([
// ...
AssignGlobalScopes::class
])
}
}
Bozz
BozzOP•14mo ago
hello sir @tuto1902 , I received your solution, but I am a beginner in phph filament. here is the nomenclature of my project. where should I put the different pieces of code that you gave me. I have attached the different files below Thank you for helping me sir.
tuto1902
tuto1902•14mo ago
Fortunately, Laravel has a great documentation. It wouldn't be a good learning experience if I write all the code for you 😉 So, give this three subjects a read. After that, go back and read the code I provided and you'll have a more clear idea on where everything goes. - Model Global Scopes https://laravel.com/docs/10.x/eloquent#writing-global-scopes - Policies https://laravel.com/docs/10.x/authorization#creating-policies - Middleware https://laravel.com/docs/10.x/middleware#defining-middleware
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Bozz
BozzOP•14mo ago
good evening sir @tuto1902 , I made the modifications in my code according to your explanation, but I have an error: """ Class "Filament\Panel\PanelServiceProvider" not found """ here are the modifications made: 1 - To display only the information of the currently logged in user in the user dashboard, I used global scopes and policies. First, I create a middleware class called AssignGlobalScopes that applies a global scope to each request. This global scope will filter data based on the currently authenticated user: class AssignGlobalScopes { public function handle(Request $request, Closure $next): Response { User::addGlobalScope(function (Builder $query) { $query->whereBelongsTo(Filament::auth()->user(), 'owner'); }); return $next($request); } } 2 - Next, I created a policy class called UserPolicy which only allows administrators to update users: class UserPolicy { public function update(): bool { return auth()->user()->role->name == 'admin'; } } 3 - I saved the policy in the AuthServiceProvider: class AuthServiceProvider extends ServiceProvider { protected $policies = [ User::class => UserPolicy::class ]; } 4 - Finally, apply the middleware and policy to your UserPanelProvider: class UserPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { return $panel // ... ->middleware([ // ... AssignGlobalScopes::class ]) ->policy(User::class, UserPolicy::class); } } I attach below the code of the modifications made
Bozz
BozzOP•14mo ago
No description
No description
Solution
tuto1902
tuto1902•14mo ago
the UserPanelProvider class should be automatically created when you create a new panel using php artisan make:filament-panel User and it should be located in app\Providers\Filament\UserPanelProvider.php It also should extend Filament\PanelProvider, not FilamentServiceProvider. And that error means that Laravel can't find the class you are extending from. Which usually means you either using the wron class name, or you didn't import the class with use
tuto1902
tuto1902•14mo ago
@Bozz Its been a few days. Do you mind if I mark this as solved?
Bozz
BozzOP•13mo ago
no, the problem has not been resolved
Bozz
BozzOP•13mo ago
I hope you're doing well. I used Laravel Breeze for user registration and login and then added a form to the Livewire component (the filament form bulder), for the user profile, but when submitting the form, the page reloads and nothing is saved in the database and the fields are reset. here are the important files thank you for helping me.
tuto1902
tuto1902•13mo ago
Looking at your code, looks like you are on Filament v2. I noticed that the documentation states that the form needs wire:submit.prevent but you are using wire:submit Maybe that's why the page is reloading.
Bozz
BozzOP•13mo ago
I use filament v3 I use filament v3
tuto1902
tuto1902•13mo ago
getFormSchema() is from v2. Filament v3 uses public function form(). Maybe the problem is that you are using deprecated methods? https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
Want results from more Discord servers?
Add your server