11

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
  • 1
    Try this : $flight = Flight::find($id); $flight->destinations = $data; $flight->save(); see here Commented Jul 27, 2017 at 22:42
  • @Maraboc post your answer please Commented Jul 28, 2017 at 7:55
  • I just realised the need to add a laravel version tag to a question. I need to do update on in my laravel project but I don't know which version this question is targeted. Commented Dec 13, 2018 at 9:14
  • In my case I had similar problem, and it appeared I had forgotten about the setDestinationsAttribute() mutator which was the real culprit. Commented Nov 25, 2022 at 15:55

4 Answers 4

15

You can access your json keys using the arrow so you can update your column like so:

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

As @fubar mentioned you have to have mysql 5.7 in order to have my solution to work.

check the docs

Sign up to request clarification or add additional context in comments.

1 Comment

This isn't supported by all databases. E.g, MySQL 5.7+.
8

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();

Comments

4

According to this conversation on Github : Make json attributes fillable if Model field is fillable Taylor Otwell recommande the use of save method :

$model->options = ['foo' => 'bar'];

$model->save();

So in you case you can do it like this :

$flight = Flight::find($id); 
$flight->destinations = $data; 
$flight->save();

1 Comment

Why is this an accepted answer, again? This has NOTHING to do with database JSON type column and an appropriate JSON_SET method!
3

You're getting that error because you're trying to update your model using the Query Builder, which basically just creates raw SQL queries. It isn't aware of any data casting, etc defined within your model. You therefore have three choices:

1) Find your model, and run the update on your model instance.

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

2) Convert the data to a string before updating.

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

3) Use a database that supports JSON column queries, per @AmrAly's suggestion. Beware of this option, as not all databases support JSON columns.

1 Comment

I know it can be done using json_encode but I wanted to use the casting feature of eloquent . thanks for the answer

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.