如何从 db 中获取数组并将其显示在 table in laravel 中
How to fetch an array from db and show it in table in laravel
嘿,我正在做一个项目,我已经将一些数据保存在一个数组中,我想在下面的 table 中显示该数组。
[{"medicine":["Azax 500 Tablet","Benadryl Syrup","Bro-Zedex Syrup"],"dosage":["1 pill","1 pill A"," 1 丸 B"],"时间":["一天两次","一天两次 A","一天两次 B"],"持续时间":["1 周","1 周 A","1 B 周"]}]
我尝试获取数据,但得到的结果并不理想。
听到是我的控制器代码
$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'));
听说是我的blade文件代码
<!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>
听到是我的输出
https://drive.google.com/file/d/1iBaE4HqbLBnj1cH56uDlUqL3UYoVfED8/view?usp=sharing
非常感谢任何帮助。谢谢
您正在迭代处方项目,并显示它们的第一个索引,
如果您的结果如您所愿,只是 table 显示不好,您必须为每个 dosage, timing, duration
使用 <td>
或者如果您的结果只显示 medicine
项而不是更多,
您必须在其他部分显示相同的索引 (dosage, timing, duration
)
因为我不知道你的确切问题,以及一个结果示例,我无法修改代码...
显示
的回复
@foreach ($Prescription as $item)
<tr>
@dd($item, $Prescription)
<td>{{ $item->Prescription[0] }}</td>
</tr>
@endforeach
完全没有明确的问题,在评论中显示结果后,使用此块代码:
@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
嘿,我正在做一个项目,我已经将一些数据保存在一个数组中,我想在下面的 table 中显示该数组。
[{"medicine":["Azax 500 Tablet","Benadryl Syrup","Bro-Zedex Syrup"],"dosage":["1 pill","1 pill A"," 1 丸 B"],"时间":["一天两次","一天两次 A","一天两次 B"],"持续时间":["1 周","1 周 A","1 B 周"]}]
我尝试获取数据,但得到的结果并不理想。 听到是我的控制器代码
$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'));
听说是我的blade文件代码
<!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>
听到是我的输出
https://drive.google.com/file/d/1iBaE4HqbLBnj1cH56uDlUqL3UYoVfED8/view?usp=sharing
非常感谢任何帮助。谢谢
您正在迭代处方项目,并显示它们的第一个索引,
如果您的结果如您所愿,只是 table 显示不好,您必须为每个 dosage, timing, duration
<td>
或者如果您的结果只显示 medicine
项而不是更多,
您必须在其他部分显示相同的索引 (dosage, timing, duration
)
因为我不知道你的确切问题,以及一个结果示例,我无法修改代码...
显示
的回复 @foreach ($Prescription as $item)
<tr>
@dd($item, $Prescription)
<td>{{ $item->Prescription[0] }}</td>
</tr>
@endforeach
完全没有明确的问题,在评论中显示结果后,使用此块代码:
@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