首页 > 解决方案 > Laravel 从多个中选择解码的 json 变量

问题描述

假设我使用 Eloquent '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”值?

标签: jsonlaravelviewlaravel-blade

解决方案


我通过浏览找到了无需 foreach 即可访问的解决方案$var->telepon_debitur[0]->no_telepon


推荐阅读