samedi 2 novembre 2019

Laravel 5.8: Error Field doesn't have a default value

I saw similar questions but non of them work for me. This is the Transition model

class Transition extends Model
{
 protected $fillable = ['origin_x', 'origin_y', 'destination_x', 'destination_y', 'freight_id', 'status', 'receiver_name', 'receiver_mobile', 'receiver_address'];
 protected $table = 'freight_transitions';
}

and this is the insertion code

    $transition = Transition::create([
        'origin_x' => $redis['origin_x'],
        'origin_y' => $redis['origin_y'],
        'destination_x' => $redis['destination_x'],
        'destination_y' => $redis['destination_y'],
        'freight_id' => $freight->id,
        'status' => 2,
        'receiver_name' => $redis['receiver_name'],
        'receiver_mobile' => $redis['receiver_mobile'],
        'receiver_address' => $redis['receiver_address']
    ]);

I am sure the array of $redis` has value. But this is the error

General error: 1364 Field 'origin_x' doesn't have a default value (SQL: insert into freight_transitions (updated_at, created_at) values (2019-11-02 16:42:58, 2019-11-02 16:42:58))

From what I see, the Laravel does not try to insert the origin_x and other fields in to the DB. it only inserts the created_at and updated_at. I have a similar model called Freight, in a few lines above this code, I insert records in the same way with no error. But I don't know why it only inserts the created_at and updated_at.

I also tried

$transition = new Transition([....]);//array of above data
$transition->save();

It also generates the same error.

This is the migration

    Schema::create('freight_transitions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('origin_x');
        $table->string('origin_y');
        $table->string('destination_x');
        $table->string('destination_y');
        $table->string('receiver_name')->nullable();
        $table->string('receiver_mobile')->nullable();
        $table->string('receiver_address')->nullable();
        $table->bigInteger('freight_id')->unsigned();
        $table->foreign('freight_id')->references('id')->on('freight_freights')->onDelete('cascade');
        $table->enum('status', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'])->default(1);//1: start
        $table->timestamps();
    });

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire