How to update a json column in laravel?

Member

by daisha , in category: PHP Frameworks , 9 months ago

How to update a json column in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 9 months 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.

Related Threads:

How to update only 3 column value in laravel?
How to update json data in a column using mysql?
How to update an exiting column on laravel?
How to update a column as foreign key on laravel?
How to update jsonb column using laravel query?
How to update json key value in laravel?