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:Jump to 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
...12 Replies
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.
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.
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.
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
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
@Bozz Its been a few days. Do you mind if I mark this as solved?
no, the problem has not been resolved
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.
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.I use filament v3
I use filament v3
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