Fahad Khan
Fahad Khan

Reputation: 375

Fetch object within an object in Blade

I want to get a value of an object within an object using Blade. I have a sample data below.

{
"id":1,
"vendor_id":27,
"invoice_no":"2017-02-05-1",
"cust_name":"Arbort",
"cust_email":"[email protected]",
"cust_mobile":"999990000",
"details":"{
            "0f0a4877fc628d5511fa0c7fd8ef19d3": {
                "id": "12802", 
                "qty": "5",
                "tax": 4357.5, 
                "name": "SAMSUNG Refrigerator",
                "price": 290,
                "subtotal": 145250}
                }
}

NOTE: It is just a single record. No eloquent functions.

Upvotes: 0

Views: 428

Answers (2)

whoacowboy
whoacowboy

Reputation: 7447

There are much better ways of doing this but you could do this from your blade template.

@php
$json = '{"id":1,"vendor_id":27,"invoice_no":"2017-02-05-1","cust_name":"Arbort","cust_email":"[email protected]","cust_mobile":"999990000","details":{"0f0a4877fc628d5511fa0c7fd8ef19d3":{"id":"12802","qty":"5","tax":4357.5,"name":"SAMSUNGRefrigerator","price":290,"subtotal":145250}}}';
$decoded = json_decode($json);
@endphp
@foreach($decoded->details as $detail)
    id: {{ $detail->id }}<br/>
    name: {{ $detail->name }}<br/>
    qty: {{ $detail->qty }}<br/>
    tax: {{ $detail->tax }}<br/>
    price: {{ $detail->price }}<br/>
    subtotal: {{ $detail->subtotal }}<br/>
@endforeach

Upvotes: 1

CJ Edgerton
CJ Edgerton

Reputation: 420

You should be able to access properties like so:

{{ $object->details->name }}

<!-- echos SAMSUNG Refrigerator -->

Upvotes: 0

Related Questions