Hey there i am working on a project and i have saved some data in an array which i would like to show in a table below is the array.
[{"medicine":["Azax 500 Tablet","Benadryl Syrup","Bro-Zedex Syrup"],"dosage":["1 pill","1 pill A","1 pill B"],"timing":["Twice a day","Twice a day A","Twice a day B"],"duration":["1 week","1 week A","1 week B"]}]
I tried getting the data but the result i got was not ideal. Hear is my controller code
$PrescId = $Request->id;
$PrescDetails = DB::table('prescriptionunrege')->where('id',$PrescId)->first();
$Prescription = DB::table('prescriptionunrege')->where('id',$PrescId)->get();
return view('test',compact('PrescDetails','Prescription'));
hear is my blade file code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>Name: {{ $PrescDetails->Name }}</div>
<div>Contact: {{ $PrescDetails->Contact }}</div>
<div>Gender: {{ $PrescDetails->Gender }}</div>
<div>Age: {{ $PrescDetails->Age }}</div>
<div>Diagnosis: {{ $PrescDetails->Diagnosis }}</div>
<div>{{ $PrescDetails->Prescription }}</div>
<div style="margin-top: 55px">
<table>
<tr>
<th>Medicine</th>
<th>Dosage</th>
<th>Timing</th>
<th>Duration</th>
</tr>
@foreach ($Prescription as $item)
<tr>
<td>{{ $item->Prescription[0] }}</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>
Hear is my output
https://drive.google.com/file/d/1iBaE4HqbLBnj1cH56uDlUqL3UYoVfED8/view?usp=sharing
any help is really appreciated. Thankyou
CodePudding user response:
You are iterating for prescription items, and showing first index of them,
if your result is expected as you want and just table is showing bad, you must use a <td>
for each of dosage, timing, duration
or if your result show only medicine
items and not more,
you must show same index in other sections (dosage, timing, duration
)
because i don't know your exact problem, and a result example, i can't modify code...
show response of
@foreach ($Prescription as $item)
<tr>
@dd($item, $Prescription)
<td>{{ $item->Prescription[0] }}</td>
</tr>
@endforeach
not clear question at all, after showing result in comments, use this block code :
@foreach ($Prescription as $item)
@php($nPrescription = json_decode($item->Prescription, true))
@foreach($nPrescription[0]['medicine'] as $index => $medicine)
<tr>
<td>{{ $medicine }}</td>
<td>{{ $nPrescription[0]['dosage'][$index] }}</td>
<td>{{ $nPrescription[0]['timing'][$index] }}</td>
<td>{{ $nPrescription[0]['duration'][$index] }}</td>
</tr>
@endforeach
@endforeach