F
Filament•4w ago
LEATHEANX

How to prefill from foreign key address

What I did: im trying to prefill the leaverequest with the employees_id that is associated with the users_id My issue/the error: it wont get the employees_id to be prefilled and to be inserted in the database and it returns a null value instead of getting the proper employee_id for the leaverequest Code Forms\Components\Hidden::make('employees_id') ->default(fn () => Filament::auth()->user()?->employee?->id) ->dehydrated(true) ->required(), in the users model public function employee() { return $this->hasOne(Employee::class, 'users_id'); } In the leave request model public function employee() { return $this->belongsTo(Employee::class); } in the employee model public function user() { return $this->belongsTo(User::class, 'users_id'); } public function leaverequest() { return $this->hasMany(Leaverequest::class, 'employees_id'); } side note: I am a beginner level in coding, as i have no prior knowledge in laravel or PHP
5 Replies
LEATHEANX
LEATHEANXOP•4w ago
the database relationship
LEATHEANX
LEATHEANXOP•4w ago
No description
LEATHEANX
LEATHEANXOP•4w ago
much appreiated if someone can help me 🙏
Dennis Koch
Dennis Koch•4w ago
Is this on create? Instead of using a Hidden field it's better to use mutateFormDataBeforeCreate() because Hidden fields can be modified. Also please read #✅┊rules to see how to format code properly on Discord
LEATHEANX
LEATHEANXOP•4w ago
Okay thank you ill try it right now also i am very sorry for bad format
public static function mutateFormDataBeforeCreate(array $data): array
{
$data['employees_id'] = Filament::auth()->user()?->employee?->id;
return $data;
}
public static function mutateFormDataBeforeCreate(array $data): array
{
$data['employees_id'] = Filament::auth()->user()?->employee?->id;
return $data;
}
i have tried it and it still returns a null value but when i check the value in the textInput to verify it, it is correct ps: It works now thank you the problem was i was trying to input to 'employee_id' instead of 'employees_id'

Did you find this page helpful?