Associate BelongsTo relationship

Hello, I'm new to using Filament. Can I ask if this is an acceptable approach when saving records with a relationship based on authenticated user data? resource: https://gist.github.com/dansingian/d1705d6bb133ffd9fca4aaed7828b5bb create: https://gist.github.com/dansingian/6a05722006d72d1ed2fed80c4890b063
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.
5 Replies
ZedoX
ZedoX2y ago
You can hook this into afterCreate? https://filamentphp.com/docs/2.x/admin/resources/creating-records#lifecycle-hooks Something like:
protected function afterCreate(): void
{
$job = $this->record;

$job->account()->associate(auth()->user()->account);
}
protected function afterCreate(): void
{
$job = $this->record;

$job->account()->associate(auth()->user()->account);
}
dansing
dansingOP2y ago
It throws an error before it proceeds on the afterCreate, giving me a "General error: 1364 Field 'account_id' doesn't have a default value". My table schema has $table->unsignedBigInteger('account_id'); $table->foreign('account_id') ->references('id') ->on('profiles_accounts') ->onUpdate('NO ACTION') ->onDelete('RESTRICT') ;
ZedoX
ZedoX2y ago
Filament
Creating records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
ZedoX
ZedoX2y ago
just set $data['account_id'] = auth()->user()->account->id ?
dansing
dansingOP2y ago
still an error, since my account_id is guarded @z3d0x btw Thank you!

Did you find this page helpful?