Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

royhara's avatar

How to get the current model in relationship manager filament

Currently I am working on a project with filament version 2, I have 3 tables like the following

| users | profiles | skills     |
| ----- | -------- | ---------- |
| id    | id       | id         |
| name  | user_id  | profile_id |
|       | image    | name       |

in App\Models\User I have a relationship like this

public function profile(): HasOne
{
   return $this->hasOne(Profile::class, 'user_id');
}

public function skills(): HasManyThrough
{
   return $this->hasManyThrough(Skill::class, Profile::class);
}

I have App\Filament\Resources\UserResource\RelationManagers\SkillRelationManager. In this file there is a SkillRelationManager class which has a static method form as follows

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Hidden::make('profile_id')
                ->default(fn(User $user) => $user->profile->id),

            Forms\Components\TextInput::make('name')
                ->required(),
        ]);
}

I want to get the profile ID of the user I am editing. I have tried using a callback as below, but the callback actually creates a new user instance instead of retrieving the user data that I am editing.

Forms\Components\Hidden::make('profile_id')
    ->default(fn(User $user) => $user->profile->id)

So how do I get the current user that I am editing

0 likes
1 reply
dualklip's avatar

Maybe you can use request inside of the function. Something like this

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Hidden::make('profile_id')
                ->default(function () {
                    $profileId = request()->route('profile_id');  // Get profile ID from current request, if it's defined in your routing, but perhaps you have to implement other kind of logic
                    return $profileId;
                }),
            
            Forms\Components\TextInput::make('name')
                ->required(),
        ]);
}

Please or to participate in this conversation.