I want to insert data by seeder:
public function run()
{
$projects = [
[
"name" => "project-one",
"images" => ["image11.jpg", "image12.jpg", "image13.jpg"],
],
[
"name" => "project-two",
"images" => ["image21.jpg", "image22.jpg", "image23.jpg"],
],
];
Project::insert($projects);
}
images column is JSON, I get an array error on images insertion. How I can insert them?
CodePudding user response:
insert() method can not use any casts of the model, it will be inserted as raw. So you just need to convert the array to a json using json_encode() function.
public function run()
{
$projects = [
[
"name" => "project-one",
"images" => json_encode(["image11.jpg", "image12.jpg", "image13.jpg"]),
],
[
"name" => "project-two",
"images" => json_encode(["image21.jpg", "image22.jpg", "image23.jpg"]),
],
];
Project::insert($projects);
}
