php - Laravel 5.6 PostGres Json 默认为空

标签 php laravel postgresql

我一次只需要插入 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/

相关文章:

PHP/MySQL 动态面包屑

php - 如何在 PHP Laravel Lumen 中建立与数据库的连接?

php - 拉维尔 5 : app() helper function

postgresql - 无法正常启动postgresql

ruby-on-rails - 如何使用 change_column 在 rails migration postgres 中使用 hstore 生成正确的 sql

php - 尝试将 Codeigniter Project 3.0 上传到主机时出现错误 404?

php按自定义顺序对多维数组进行排序

php - laravel 中的 API 版本控制 : routing depending on the "Accept" Header

php - 如何根据数量更新商品的总价?

ruby-on-rails - Rails/Postgres 连接问题(psql 和 pg 工作正常)