如何从DB中获取阵列并在Laravel的桌子中显示

发布于 2025-01-23 23:58:52 字数 2054 浏览 0 评论 0原文

嘿,我正在研究一个项目,并且我想在一个数组中保存了一些数据,我想在下表中显示这些数据。

[{“ Medicine”:[“ Azax 500片剂”,“ Benadryl糖浆”,“ Bro-Zedex糖浆”],“剂量”:[“ 1丸”,“ 1丸”,“ 1丸”,“ 1丸B”,“计时”:[“每天两次”,“每天两次A”,“每天两次”,“ b”],“持续时间”:[“ 1周”,“ 1周A”,“ 1周B”]}]

I尝试获取数据,但我得到的结果并不理想。 听到我的控制器代码

$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'));

听到的是我的刀片文件代码

<!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>

听到我的输出是我的输出

任何帮助都非常感谢。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-01-30 23:58:52

您正在迭代处方项目,并显示它们的第一个索引,

如果您的结果是您想要的,并且仅显示不良表现不好,则必须使用剂量,时机,持续时间

或您的结果仅显示Medicine项目,而不是更多,

则必须在其他部分中显示相同的索引(剂量,计时,时间,持续时间

因为我不知道您的确切问题,也是一个结果示例,我无法修改代码...

的回答,在注释中显示结果后,请使用此块代码:

            @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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文