【发布时间】: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