2

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
jones
  • 1,423
  • 3
  • 35
  • 76

3 Answers3

3

Form laravel github issues:

That's just a helper for the most simple case for defining both the morph id and morph type columns. If you need more control, they can be assigned individually.

You're better off doing it manually,

$table->unsignedInteger('attachable_id')->nullable();
$table->string('attachable_type')->nullable();
N69S
  • 16,110
  • 3
  • 22
  • 36
  • @jones did you check directly in your DB that the field is marked as nullable ? – N69S Sep 16 '19 at 09:50
  • In my DB, its default value is null – jones Sep 16 '19 at 09:53
  • @jones then how are you getting `Field 'attachable_id' doesn't have a default value`. cause that's an error from the DB. – N69S Sep 16 '19 at 09:56
  • Got it, my test_db not updated, because temporary I've commented `RefreshDatabase`, and new changes to migration not affected on that. I've checked that development db is changed, but test db is not and found the reason. – jones Sep 16 '19 at 09:59
  • @jones if you're doing it manually, check if the Ids you're referencing are `unsignedBigInteger` or `unsignedInteger`. – N69S Sep 16 '19 at 10:00
  • For the record, `nullable()` results in a `default null` column spec, and the `nullableMorphs()` helper does create `nullable()` fields. So you don't need to do this manually – tanerkay Sep 16 '19 at 10:10
1

Use attachable_id in fillable array in Attachment model.

class Attachment extends Model
{
    protected $fillable = ['attachable_id'];
}

Note: Make sure in table attachable_id is nullable.

Priyanka khullar
  • 509
  • 1
  • 5
  • 25
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
0

You will need to specify fillable attribute on the model

class Attachment extends Model
{
      //only the field names inside the array can be mass-assign
      protected $fillable = ['attachable_id']; 
}

More details: What does “Mass Assignment” mean in Laravel (Link)


In your migration you can do this to make the column nullable:

public function up()
{
    Schema::create('tablename', function (Blueprint $table) {
        $table->increments('attachable_id')->nullable();
    });
}

->nullable() Designate that the column allows NULL values (Link)

Priyanka khullar
  • 509
  • 1
  • 5
  • 25
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64