【问题标题】:Laravel 5.8: Error Field doesn't have a default valueLaravel 5.8:错误字段没有默认值
【发布时间】:2019-11-02 16:52:36
【问题描述】:

我看到了类似的问题,但没有一个对我有用。这是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_transitions(updated_at,created_at)值(2019-11-02 16:42:58,2019- 11-02 16:42:58))

据我所知,Laravel 不会尝试将 origin_x 和其他字段插入数据库。它只插入created_at 和updated_at。我有一个类似的模型,称为Freight,在这段代码上方的几行中,我以相同的方式插入记录,没有错误。但我不知道为什么它只插入created_at和updated_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();
    });

这是redis json 格式的数组

{
"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
}

提前致谢。

【问题讨论】:

  • 你能说明$redis数组的来源吗?
  • @Saly3301 我确信它具有来自dd 的价值。它来自 Redis 服务器包。如您所见,错误是因为它没有尝试为origin_x 和其他字段插入任何值
  • 我无法仅使用您提供的代码进行复制,请添加dd($redis);的输出
  • 是return $redis;还是dd($redis);的输出? $redis 是对象还是数组?

标签: laravel laravel-5 eloquent


【解决方案1】:

我尝试如下插入数据

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

即使它有效,但对我来说仍然是一个问题,为什么我用于大量项目的代码在这里不起作用:|

【讨论】:

  • 我发现最好不要标记任何受保护的字段而不是所有字段都可以填写,即protected $guarded = [];
  • @Saly3301 Laravel 文档强烈建议将字段保存在fillable
  • Laravel 文档建议不要使用 Mass Assignment
  • @Saly3301 你是对的。但这里有一点。 Fillable 可让您指定模型中可批量分配的字段。因此,通过在 Fillable 中指定它们必须没有错误
【解决方案2】:

如果没有使用 php 的 json_decode 函数将其转换为数组,您是否将 json 转换为数组,然后再试一次。如果可能,然后为您的列提供默认值。

【讨论】:

  • 请将此作为对问题的评论。如果您可以通过随附的代码给出更详细的答案,请放在这里。
  • 代码$transition = Transition::create([ ... ])清楚地表明数据在数组中。
  • 我问你,没有把json转成数组吗?
  • json只是为了展示数据。数据实际上是一个数组
  • create 用于将数据以数组格式保存到数据库中。我们必须将值作为数组传递来创建函数
猜你喜欢
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 2013-03-04
相关资源
最近更新 更多