0

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?

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.