F
Filamentβ€’4mo ago
Bruno Gomes

I want to use filament as a personal dashboard. Any recommendations on how to go about it?

Hi, I'm new to filamentphp but find it amazing. I was wondering if I can use it to make not a general dashboard but rather a personal one, meaning not to have to choose a user when creating a resource etc as it should be the logged in one. Let's go with the classic to-do example. I want to be able to add tasks for ME as the logged in user. And if a friend create an account they will add to their account, etc etc. How to go about it? Any tips or recommendations of documentations, tutorials, etc?
16 Replies
awcodes
awcodesβ€’4mo ago
Create a custom dashboard page https://filamentphp.com/docs/3.x/panels/dashboard#customizing-the-dashboard-page and use the logged in user for anything you need in the view.
Bruno Gomes
Bruno GomesOPβ€’4mo ago
Hi, thanks for the quick answer!
Bruno Gomes
Bruno GomesOPβ€’4mo ago
Won't it just affect that specific page? Maybe I've phrased myself wrong... This is an example of what I mean:
No description
Bruno Gomes
Bruno GomesOPβ€’4mo ago
I wish I didn't have to choose a user there, it should automatically go to me
awcodes
awcodesβ€’4mo ago
You don’t have to choose a user, it’s already defined by the laravel auth guard So, at that point any interaction is defined by the lavavel policy.
zaidpirwani
zaidpirwaniβ€’4mo ago
you need to modify the form, remove the user select field and instead maybe use a hidden field with auth()->user()->id or add user_id to form data before creation using life cycle hooks. Use a global Scope to make sure people only see THEIR tasks.
Bruno Gomes
Bruno GomesOPβ€’4mo ago
Hmmm okay, I was thinking of something like that as well but was like "what if there's already a standard way to make this happen and I'm missing out?" πŸ˜… But having u suggest the same makes me think I'm not on the wrong here so that's what I'm gonna go with. Thank you!
Dennis Koch
Dennis Kochβ€’4mo ago
You can leave out the User form field and use a Observer or the creating() hook for Eloquent models to always prefill the current user id for that model.
toeknee
toekneeβ€’4mo ago
Depending on the usecase of the models, you can chuck it in the boot function too:
public static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->user_id = auth()->user()->id;
});
}
public static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->user_id = auth()->user()->id;
});
}
When it is being created and only created, it will assign the authorised users ID.
Dennis Koch
Dennis Kochβ€’4mo ago
That's what I meant, but thanks for the code example πŸ˜…
toeknee
toekneeβ€’4mo ago
Anytime πŸ˜‰
Bruno Gomes
Bruno GomesOPβ€’4mo ago
Wow, thank you guys so much for the tips! Also, now that you've mentioned it, wouldn't this be a good addition to v4? Like having this kind of behavior be enabled with maybe a couple of method calls on the panelProvider or something similar would be pretty neat! Albeit that I have no Idea how complicated would it be to implement πŸ˜…
awcodes
awcodesβ€’4mo ago
It’s already possible. πŸ˜€
toeknee
toekneeβ€’4mo ago
@Bruno Gomes so scoping is already part of V4 if you enable it? scopeToTenant = true on the resource. OR scoped to tnenant by default, tenant being users I'd say. But personally? I've just build a trait if you want to apply to a lot:
trait HasCreator
{
protected static function bootHasCreator(): void
{
static::creating(function ($model) {
if (Auth::check()) {
$model->user_id = Auth::user()->id;
}
});
}
}
trait HasCreator
{
protected static function bootHasCreator(): void
{
static::creating(function ($model) {
if (Auth::check()) {
$model->user_id = Auth::user()->id;
}
});
}
}
then add to each model:
use HasFactory, HasCreator;
use HasFactory, HasCreator;
Bruno Pereira
Bruno Pereiraβ€’4mo ago
wrong @ xD
toeknee
toekneeβ€’4mo ago
soz!

Did you find this page helpful?