0

我想遍历 Laravel 中的嵌套对象并获取刀片文件的值。

样本对象:

[
    {
        "id": 43,
        "user_id": 2,
        "event": "updated",
        "auditable_id": 34,
        "auditable_type": "App\\Bill",
        "old_values": {
            "message": "test messgare",
            "napprover": "24",
            "status": "Added"
        },
        "new_values": {
            "message": "Second Audit",
            "napprover": "10001",
            "status": "Bill Processing"
        },
        "url": "http://localhost:8000/admin/bills/34",
        "ip_address": "127.0.0.1",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3186.0 Safari/537.36",
        "created_at": "2017-08-16 18:44:12"
    },
    {
        "id": 41,
        "user_id": 2,
        "event": "created",
        "auditable_id": 34,
        "auditable_type": "App\\Bill",
        "old_values": [],
        "new_values": {
            "bill_no": "2017081527",
            "bill_type": "BILL",
            "bill_date": "08/15/2017",
            "vendor_id": "2563582",
            "priority": "Moderate Priority",
            "amount": "125245",
            "message": "test messgare",
            "initiator": "10001",
            "napprover": "10003",
            "tat": "1",
            "status": "Added",
            "department": "6",
            "created_at": "2017-08-15 11:57:45"
        },
        "url": "http://localhost:8000/admin/bills",
        "ip_address": "127.0.0.1",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3185.0 Safari/537.36",
        "created_at": "2017-08-15 11:57:45"
    }
]

控制器:

public function show($id)
{
    $bill   = Bill::findOrFail($id);
    $vendor = $bill->vendor_id;
    $user   = User::where('vendor_id', $vendor)->get();
    $all    = $bill->audits()->get();
    return view('admin.BillDetail', compact('bill','user','all'));
}

刀片文件:

@foreach($all as $al)
    @foreach($all as $a)
    <li>{{$a}}</li>
    @endforeach
@endforeach

我想访问old_valuesnew_values反对

如何在循环中获取这些值

4

1 回答 1

1

正如我看到您的对象样本old_values&new_values是单个对象而不是对象数组,因此您可以按以下方式进行操作:

@foreach($all as $al)
    @if(isset($al->new_values))
    <li>{{$al->new_values->message}}</li>
    @endif
@endforeach

注意:如果存在 null 或空对象的可能性,那么您需要在访问对象的任何属性之前添加一些检查,否则它将通过异常。

于 2017-08-17T06:05:09.857 回答