Laravel 5.8:“错误字段”没有默认值

时间:2019-11-02 16:52:36

标签: laravel laravel-5 eloquent

我看到了类似的问题,但没有一个对我有用。这是Transition模型

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';
}

这是插入代码

    $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']
    ]);

我确定$ redis`数组具有价值。但这是错误

  

一般错误:1364字段'origin_x'没有默认值(SQL:插入freight_transitionsupdated_atcreated_at)值(2019-11-02 16: 42:58,2019-11-02 16:42:58))

据我所知,Laravel不会尝试将origin_x和其他字段插入数据库。它仅插入created_atupdated_at。我有一个名为Freight的相似模型,在此代码上方的几行中,我以相同的方式插入了记录,没有错误。但我不知道为什么只插入created_atupdated_at

我也尝试过

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

它也会产生相同的错误。

这是迁移

    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();
    });

这是json格式的redis数组

{
"origin_x": "555",
"origin_y": "666",
"destination_x": "777",
"destination_y": "8888",
"freight_type_id": "1",
"title": null,
"description": null,
"price": 130000,
"features": "2",
"services": "2,5",
"vehicle_id": 1,
"token": "111111",
"payment_type": "3",
"user_id": 1,
"receiver_name": null,
"receiver_mobile": null,
"receiver_address": null,
"payment_status": 1,
"status": 2
}

谢谢。

2 个答案:

答案 0 :(得分:0)

我尝试如下插入数据

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

尽管它可以工作,但是对我来说仍然是一个问题,为什么我在大量项目中使用的代码在这里不起作用:|

答案 1 :(得分:-2)

如果未使用php的json_decode函数将json转换为数组,是否将json转换为数组,然后重试。如果可能的话,请为您的列提供默认值。