F
Filament14mo ago
riz

afterCreate() function can't get the last saved record id, dd() show the error below:

ERROR: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'approvable_id' cannot be null (Connection: mysql, SQL: insert into approval (status, approvable_id, approvable_type, updated_at, created_at) values (Pending, ?, App\Models\Applicant, 2023-10-20 10:35:02, 2023-10-20 10:35:02)) ◀" // app\Filament\Resources\ApplicantResource\Pages\CreateApplicant.php:27 protected function afterCreate(): void {
try {
$this->record->approval()->create(['status' => 'Pending']);
} catch (\Throwable $th) { dd($th->getMessage()); //$this->record->forceDelete(); } }
3 Replies
riz
rizOP14mo ago
dd($this->record->id); shows null dd($this->record->getKey()); is also null, but I can get $this->record->created_at and other fields except the record id dd($this->record); also shows below App\Models\Applicant {#2976 ▼ // app\Filament\Resources\ApplicantResource\Pages\CreateApplicant.php:23 #connection: "mysql" #table: "award_applicant"
cheesegrits
cheesegrits14mo ago
Try $this->record-refresh(), see if it picks up the id.
riz
rizOP13mo ago
if I declare the model like this: class Model extends Pivot then the protected function afterCreate(): void { dd($this->record->id); } shows null, if I check the database, a new record is already added with the id using $this->record-refresh() inside the afterCreate(), i get an error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause',
Want results from more Discord servers?
Add your server