properties
我有一个包含一列的表json-данные
$table->json('properties')->nullable();
在Seeder
你需要添加的文件中json-данные
$sections = [
[
'name' => 'section1',
'content' => 'content1',
'properties' => json_encode([
"link" => "/link"
]),
],
...
DB::table('sections')->insert($sections);
但是在导入数据时,会出现以下错误:
Insert value list does not match column list: 1136 Column count doesn't match value count at row 2
如果删除json_encode
,它会给出一个错误:
Array to string conversion
如何正确插入json-данные
?
第一个错误说插入值的数量与列数不匹配。看起来您还有一个错误,而不是在第一个数据数组中。检查整个数据数组
第二个错误是正确的。你需要做数组编码。
我通常通过一个模型插入数据
Sections::create($section)
,其中描述给定字段是 json,在这种情况下,我不对字段进行编码,而是将其保留为数组。并且没有问题。