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

miquelangeld's avatar

Filament Relation Manager with a relationship with same model

Hi, I have a model named JobPosition, the model has two fields 'name','parent_id'

And a relationship named children in order to nest (Determine which Job positions are managing another job positions, the tipical category/subcategory scheme). I created a Relation Manager which allow me to create a relationship, but if I want to attach then I get this error: Call to undefined method App\Models\JobPosition::jobPositions() I don't understand why is calling a method which is not even defined.

Thanks in advance.

My model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class JobPosition extends Model
{
    protected $fillable = ['name','rrhhid','parent_id'];

    public function employees(): hasMany
    {
        return $this->hasMany(Employee::class);
    }

    public function children(): hasMany
    {
        return $this->hasMany(JobPosition::class, 'parent_id');
    }

}

My relation manager

0 likes
1 reply
miquelangeld's avatar
miquelangeld
OP
Best Answer
Level 1

Ok, I answer my self, Attach method works with many to many, so I created a custom action:

Action::make('attach')
                    ->label('Attach Existing')
                    ->form([
                        Select::make('job_position_id')
                            ->label('Job Position')
                            ->options(
                                JobPosition::query()
                                    ->pluck('name', 'id')
                            )
                            ->searchable()
                            ->required(),
                    ])
                    ->action(function (array $data) {
                        JobPosition::find($data['job_position_id'])->update(['parent_id' => $this->ownerRecord->id]);
                    }),

Also I created a detach custom action:

 Action::make('detach')
                    ->label('Detach')
                    ->action(function (JobPosition $record) {
                        $record->update(['parent_id' => null]);
                    }),

If you need to create multiple relations on the same form then this is my approach:

Action::make('attach')
                    ->label('Attach Existing')
                    ->form([
                        Select::make('job_position_id')
                            ->label('Job Position')
                            ->multiple()
                            ->options(
                                JobPosition::query()
                                    ->pluck('name', 'id')
                            )
                            ->searchable()
                            ->required(),
                    ])
                    ->action(function (array $data) {
                        foreach ($data['job_position_id'] as $job_position_id) {
                            JobPosition::find($job_position_id)->update(['parent_id' => $this->ownerRecord->id]);
                        }
                    }),

Please or to participate in this conversation.