Laravel eloquent does not update JSON column : Array to string conversion

Viewed 20493

I want to update a JSON column in my database but I get this error :

Array to string conversion  

I have declared the column name as array in the model :

protected $casts = [
    'destinations' => 'array'
];

this is the code that I use :

$data[] = [
    'from' => $fromArray,
    'to' => $toArray
];

Flight::where('id', $id)->update(['destinations' => $data]);

What should I do ?

4 Answers

This code did the work for me.

$user = User::where('id', $request->user_id)
        ->first();

$array_data = ["json_key3"=>"value"];

$user->forceFill([
    'db_column->json_key1->json_key2' => $array_data
])->save();
Related