How to update a json column in laravel?

Member

by daisha , in category: PHP Frameworks , 13 days ago

How to update a json column in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 12 days ago

@daisha 

To update a JSON column in Laravel, you can use the update method on the model and pass the new JSON data as an array. Here is an example:

1
2
3
4
5
6
7
// Find the record you want to update
$record = ModelName::find($id);

// Update the JSON column with new data
$record->update([
    'json_column' => ['key1' => 'value1', 'key2' => 'value2']
]);


You can also directly access the JSON column on the model and update it like this:

1
2
3
4
5
6
// Find the record you want to update
$record = ModelName::find($id);

// Update the JSON column with new data
$record->json_column = ['key1' => 'value1', 'key2' => 'value2'];
$record->save();


Make sure to replace ModelName with the name of your model and json_column with the name of your JSON column.