Guarded foreign id

I wonder how will you associate a guarded/not in fillable foreign id when creating a new record.
4 Replies
F alko
F alko2y ago
I think you should look into the Lifecycle hooks. https://beta.filamentphp.com/docs/3.x/panels/resources/creating-records#lifecycle-hooks You probably want to set these id's in the beforeCreate lifecycle hook. Untested, but probably you want to do something like this:
protected function beforeCreate(): void
{
$currentUser = auth()->user();
$this->getRecord()->author()->associate($currentUser);
// or
$this->getRecord()->user_id = $currentUser->id;
}
protected function beforeCreate(): void
{
$currentUser = auth()->user();
$this->getRecord()->author()->associate($currentUser);
// or
$this->getRecord()->user_id = $currentUser->id;
}
wyChoong
wyChoong2y ago
Can you clarify your question. Are you asking how to do something in filament? Or how is filament handling table relationship?
wyChoong
wyChoong2y ago
If the later, it’s Laravel eloquent, read more here https://laravel.com/docs/10.x/eloquent#mass-assignment
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.
dansing
dansingOP2y ago
Gist
JobResource
JobResource. GitHub Gist: instantly share code, notes, and snippets.
Gist
Create Job Record
Create Job Record. GitHub Gist: instantly share code, notes, and snippets.

Did you find this page helpful?