how to save dynamic input data into database as json ?

Published 2 months ago by tjnapster555

I have a model invoice in which i want to save med_list as a json and retrive it from database and show it on blade as a table.

from html this is how the data coming

<input type="text" class= id="Med_id" name="Med_id[]" value="" placeholder="Med Id">
<input type="text" class= id="Med_Name" name="Med_Name[]" value="" placeholder="Med_Name">
<input type="text" class= id="Med_Price" name="Med_Price[]" value="" placeholder="Med_Price">
<input type="text" class= id="Med_Qty" name="Med_Qty[]" value="" placeholder="Med_Qty">

the data i want to save in db as json on string column its mysql 5.5 so its not natively support json

the data should be like this

{
'Med_order1':{
'Med_id':1,
'Med_Name':"Med_Name1",
'Med_Price':"23.4",
"Med_Price":5
    }
'Med_order2':{
'Med_id':2,
'Med_Name':"Med_Name2",
'Med_Price':"43.4",
"Med_Price":2
    }
}

on blade side after retrieve from db i want to show them in a table

<table>
<tr>
<td>Med_id</td>
<td>Med_Name</td>
<td>Med_Price</td>
<td>MEd_Qty</td>
</tr>
<td>1</td>
<td>Med_Name1</td>
<td>23.4</td>
<td>5</td>
</tr>
<td>2</td>
<td>Med_Name2</td>
<td>43.4</td>
<td>3</td>
</tr>
</table>
jlrdw
jlrdw
2 months ago (196,160 XP)

Makes no sense storing

{
'Med_order1':{
'Med_id':1,
'Med_Name':"Med_Name1",
'Med_Price':"23.4",
"Med_Price":5
    }
'Med_order2':{
'Med_id':2,
'Med_Name':"Med_Name2",
'Med_Price':"43.4",
"Med_Price":2
    }
}

that format in a mysql table, unles I misunderstood.

Makes sense storing that type data normal. But you can loop / foreach a json array.

Json is just an array, nothing magic.

tjnapster555

@jlrdw can you make a suggestion how i should save my medicine order in database ?

jlrdw
jlrdw
2 months ago (196,160 XP)

A related table. An order can have many items. So a master detail (one to many). https://laravel.com/docs/5.4/eloquent-relationships#one-to-many

Robstar

You could store the raw JSon object using the newer json column type in Mysql. However, the best course is to store each record in a related table.

Please sign in or create an account to participate in this conversation.