我试图添加种子类别和翻译,但我得到这个错误?任何建议如何解决这个问题?所以我想尝试的是每个类别添加两个翻译。
public function run()
{
$now = Carbon::now('utc')->toDateTimeString();
DB::table('category_translations')->delete();
DB::table('categories')->delete();
$arg1 = [
[
'slug' => 'Kreis-1-Altstadt',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-2',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-3',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-4',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-5-Industriequartier',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-6',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-7',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-8-Reisbach',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-9',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-10',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-11',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'slug' => 'Kreis-12-Schwamendingen',
'status' => 1,
'created_at' => $now,
'updated_at' => $now
]
];
$category = Categories::create($arg1);
$arg2 = [
[
'title' => 'Kreis 1 / Altstadt',
'language_id' => 1,
'category_id' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 1 / Altstadt',
'language_id' => 2,
'category_id' => 1,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 2',
'language_id' => 1,
'category_id' => 2,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 2',
'language_id' => 2,
'category_id' => 2,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 3',
'language_id' => 1,
'category_id' => 3,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 3',
'language_id' => 2,
'category_id' => 3,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 4',
'language_id' => 1,
'category_id' => 4,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 4',
'language_id' => 2,
'category_id' => 4,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 5 / Industriequartier',
'language_id' => 1,
'category_id' => 5,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 5 / Industriequartier',
'language_id' => 2,
'category_id' => 5,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 6',
'language_id' => 1,
'category_id' => 6,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 6',
'language_id' => 2,
'category_id' => 6,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 7',
'language_id' => 1,
'category_id' => 7,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 7',
'language_id' => 2,
'category_id' => 7,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 8 / Reisbach',
'language_id' => 1,
'category_id' => 8,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 8 / Reisbach',
'language_id' => 2,
'category_id' => 8,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 9',
'language_id' => 1,
'category_id' => 9,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 9',
'language_id' => 2,
'category_id' => 9,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 10',
'language_id' => 1,
'category_id' => 10,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 10',
'language_id' => 2,
'category_id' => 10,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 11',
'language_id' => 1,
'category_id' => 11,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 11',
'language_id' => 2,
'category_id' => 11,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 12 / Schwamendingen',
'language_id' => 1,
'category_id' => 12,
'created_at' => $now,
'updated_at' => $now
],
[
'title' => 'Kreis 12 / Schwamendingen',
'language_id' => 2,
'category_id' => 12,
'created_at' => $now,
'updated_at' => $now
]
];
CategoryTranslations::create($arg2);
$this->command->info(count($data).' article categories are created');
}
答案 0 :(得分:3)
create()方法接受单个模型的属性数组。如果你想创建多个对象,你需要创建()多次:
foreach($arg1 as $data) {
Categories::create($data);
}
答案 1 :(得分:0)
您也可以使用insert
Categories::insert($arg1);