Laravel使用

时间:2018-08-01 06:12:48

标签: json laravel view laravel-blade

说我使用雄辩的“ with”从数据库中检索了以下内容。

类似Debitur :: find(7)-> with('teleponDebitur')-> first();

以下是数据的结构方式:

{
                    "id": 7,
                    "user_id": 26,
                    "nama": "Mr. Zachery Wisoky",
                    "alamat": "44967 Anissa Parkways Apt. 262\nSelmerland, MS 41916-7633",
                    "tanggal_lahir": "1997-04-19",
                    "tempat_lahir": "51697 Jakubowski Freeway Apt. 414\nLake Sydneeshire, OR 11599",
                    "no_ktp": "41",
                    "created_at": "2018-07-16 02:49:34",
                    "updated_at": "2018-07-16 02:49:34",
                    "telepon_debitur": [
                        {
                            "id": 21,
                            "debitur_id": 7,
                            "no_telepon": "6",
                            "created_at": "2018-07-16 02:51:17",
                            "updated_at": "2018-07-16 02:51:17"
                        },
                        {
                            "id": 32,
                            "debitur_id": 7,
                            "no_telepon": "8701",
                            "created_at": "2018-07-16 02:51:18",
                            "updated_at": "2018-07-16 02:51:18"
                        }
                    ]
}

然后我解码json:

$value = json_decode($data);
    @foreach($value->data as $var)
    <tr>
        <td>{{ $var->id }}</td>
        <td>{{ $var->nama }}</td>
        <td>{{ $var->alamat }}</td>
        <td>{{ $var->telepon_debitur[0]->no_telepon }}</td> ????
    </tr>

很好,但是我不知道如何在'telepon_debitur'中访问值'no_telepon'?

2 个答案:

答案 0 :(得分:0)

编写另一个循环。.看起来像telepon_debitur在您的Debitur模型中定义为 hasMany 关系

 @foreach($value->data as $var)
<tr>
    <td>{{ $var->id }}</td>
    <td>{{ $var->nama }}</td>
    <td>{{ $var->alamat }}</td>
    @foreach($var->telepon_debitur as telepon)
    <td>{{ $telepon->no_telepon }}</td> 
</tr>

答案 1 :(得分:0)

通过$var->telepon_debitur[0]->no_telepon

,我发现了无需foreach即可访问的解决方案