1

When I get info from API and want to fill it in my database, I get an error:

array to string conversion

Note: My table is json type for array from database.

Controller:

    public function fetch()
    {

        $response = Http::get('some api');
        $countries = json_decode($response, true);


        foreach ($countries as $country)
        {
            $api = new Country();
            $api['code'] = $country['code'];
            $api['name'] = ['en' => $country['name']['en'], 'ka' => $country['name']['ka']];

            $api->save();
        }

        return 'DONE';
    }

I laso tried otherwise, like, json_decode($reponse->body()) and then $api->name = $country->name->en and etc...

1 Answer 1

2

I believe that this save handling alternative should work:

foreach ($countries as $country)
{
    $api = new Country();
    $api->code = $country['code'];
    $api->name = json_encode(['en' => $country['name']['en'], 'ka' => $country['name']['ka']]);

    $api->save();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.