Check the "Array Casting" section https://laravel.com/docs/5.2/eloquent-mutators
Apr 27, 2016
5
Level 2
How to save JSON data to database?
Hello, I have some difficulties on how can I save my json data to database, can someone help me about this?
In my controller I have this code for saving ..
$banks = $data['banks']; //this is my json data
$banks_data = (array) json_decode($banks); //converted as array
//inside my json file I have data for bank_code and bank_name
$payment_types = PaymentBank::create([
'bank_code' => Str::upper(trim($banks_data['bank_code'])),
'bank_name' => Str::upper(trim($banks_data['bank_name'])),
'auid' => Auth::user()->id,
]);
Any help will be appreciated. Thanks
Level 2
@tykus_ikus thanks for the idea I used the $banks_data = json_decode($banks, true); then I loop the banks to get all the data
foreach($banks_data as $bank) {
$payment_bank = PaymentBank::create([
'bank_code' => Str::upper(trim($bank['bank_code'])),
'bank_name' => Str::upper(trim($bank['bank_name'])),
]);
}
Please or to participate in this conversation.