我一次只需要插入 1 个 JSON 数组,因此如果没有条目,则需要将 JSON 数组默认为 null。我有办法做到这一点吗?
使用来自:https://github.com/laravel/framework/issues/16107 的方法
我的迁移是
Schema::create('job_details', function (Blueprint $table) {
$table->integer('id')->default('0');
$table->primary('id');
$table->foreign('id')->references('id')->on('job_searches');
$table->json('Location_of_job');
$table->json('API_URL');
$table->json('Redirected_URL');
$table->json('Description');
$table->json('Salary',6,2);
$table->timestamps();
});
型号:
protected $primaryKey = 'id';
public $timestamps = true;
protected $fillable = ['API_URL', 'Redirected_URL','Location_of_job','Description','Salary',];
protected $casts = [
'Location_of_job' => 'array',
'API_URL' => 'array',
'Redirected_URL' => 'array',
'Description' => 'array',
'Salary' => 'array'
];
protected $attributes = array(
'json' => '{}'
);
获取错误:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "Location_of_job" violates not-null constraint DETAIL: Failing row contains (0, null, null, null, null, null, null, null). (SQL: insert into "job_details" ("API_URL") values (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ())
最佳答案
来自 the docs :
To make the column "nullable", you may use the
nullable
method
$table->json('Location_of_job')->nullable();
$table->json('API_URL')->nullable();
$table->json('Redirected_URL')->nullable();
$table->json('Description')->nullable();
$table->json('Salary', 6, 2)->nullable();
关于php - Laravel 5.6 PostGres Json 默认为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794468/