I have a table for attachments, that has polymorphic relation with other tables. I want to upload a file on file select and insert it to the attachments
table, but this record is not related to any parent table until the parent record is created, so for this two fields attachable_id
, and attachable_type
should be nullable.
Following is attachments table migration:
schema::table('attachments', function (Blueprint $table) {
$table->nullableMorphs('attachable');
});
But when I create the attachment record, it shows an error.
$attachment = new Attachment();
$attachment->name = 'name';
.........
.........
$attachment->save();
"message": "SQLSTATE[HY000]: General error: 1364 Field 'attachable_id' doesn't have a default value