How to update jsonb column using laravel query?

by tressie.damore , in category: PHP Frameworks , 5 months ago

How to update jsonb column using laravel query?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 5 months ago

@tressie.damore 

You can update a JSONB column in Laravel by using the DB facade to execute a raw SQL query. Here's an example of how you can update a JSONB column in a table using Laravel:

1
2
3
4
5
use IlluminateSupportFacadesDB;

DB::table('your_table_name')
   ->where('column_name->your_json_key', 'your_condition')
   ->update(['column_name->your_json_key' => 'new_value']);


In the above example, your_table_name is the name of your table, column_name is the name of the JSONB column you want to update, your_json_key is the key of the JSON object you want to update, your_condition is the condition you want to use to filter the rows to be updated, and new_value is the new value you want to update the JSON key with.


Make sure to replace the placeholders with actual values from your application.