This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I am getting the server response in json format to my laravel controller. In my controller decoding the json data and passing to my view. In laravel blade i am unable to access data.
When i am printing total decoded result it is some thing like below.
I tried to access this by $result->shipping_address , $result['shipping_address']. But none of them worked.
JSON RESULT is like below
[
{"data":
{ "entity_id":"77",
"state":"new",
"status":"pending",
"coupon_code":null,
"protect_code":"ab0fc2890c4c9cc93d7d4586e5dcd8ff",
"shipping_description":"Flat Rate - Fixed",
"is_virtual":"0",
"store_id":"1",
"customer_id":"2",
"base_discount_amount":"0.0000",
"base_discount_canceled":null,
"base_discount_invoiced":null,
"base_discount_refunded":null,
"base_grand_total":"2146.2300",
"base_shipping_amount":"5.0000",
"base_shipping_canceled":null,
"base_shipping_invoiced":null,
"base_shipping_refunded":null,
"base_shipping_tax_amount":"0.0000",
"base_shipping_tax_refunded":null,
"base_subtotal":"1846.6800",
"base_subtotal_canceled":null,
"base_subtotal_invoiced":null,
"base_subtotal_refunded":null,
"base_tax_amount":"294.5500",
"base_tax_canceled":null,
"base_tax_invoiced":null,
"base_tax_refunded":null,
"base_to_global_rate":"1.0000",
"base_to_order_rate":"1.0000",
"base_total_canceled":null,
"base_total_invoiced":null,
"base_total_invoiced_cost":null,
"base_total_offline_refunded":null,
"base_total_online_refunded":null,
"base_total_paid":null,
"base_total_qty_ordered":null,
"base_total_refunded":null,
"discount_amount":"0.0000",
"discount_canceled":null,
"discount_invoiced":null,
"discount_refunded":null,
"grand_total":"2146.2300",
"shipping_amount":"5.0000",
"shipping_canceled":null,
"shipping_invoiced":null,
"shipping_refunded":null,
"shipping_tax_amount":"0.0000",
"shipping_tax_refunded":null,
"store_to_base_rate":"0.0000",
"store_to_order_rate":"0.0000",
"subtotal":"1846.6800",
"subtotal_canceled":null,
"subtotal_invoiced":null,
"subtotal_refunded":null,
"tax_amount":"294.5500",
"tax_canceled":null,
"tax_invoiced":null,
"tax_refunded":null,
"total_canceled":null,
"total_invoiced":null,
"total_offline_refunded":null,
"total_online_refunded":null,
"total_paid":null,
"total_qty_ordered":"1.0000",
"total_refunded":null,
"can_ship_partially":null,
"can_ship_partially_item":null,
"customer_is_guest":"0",
"customer_note_notify":"1",
"billing_address_id":"76",
"customer_group_id":"1",
"edit_increment":null,
"email_sent":null,
"send_email":"1",
"forced_shipment_with_invoice":null,
"payment_auth_expiration":null,
"quote_address_id":null,
"quote_id":"72",
"shipping_address_id":"75",
"adjustment_negative":null,
"adjustment_positive":null,
"base_adjustment_negative":null,
"base_adjustment_positive":null,
"base_shipping_discount_amount":"0.0000",
"base_subtotal_incl_tax":"2141.2300",
"base_total_due":"2146.2300",
"payment_authorization_amount":null,
"shipping_discount_amount":"0.0000",
"subtotal_incl_tax":"2141.2300",
"total_due":"2146.2300",
"weight":"0.0000",
"customer_dob":null,
"increment_id":"000000157",
"applied_rule_ids":null,
"base_currency_code":"INR",
"customer_email":"test#gmail.com",
"customer_firstname":"ridha",
"customer_lastname":"Gatto",
"customer_middlename":null,
"customer_prefix":null,
"customer_suffix":null,
"customer_taxvat":null,
"discount_description":null,
"ext_customer_id":null,
"ext_order_id":null,
"global_currency_code":"INR",
"hold_before_state":null,
"hold_before_status":null,
"order_currency_code":"INR",
"original_increment_id":null,
"relation_child_id":null,
"relation_child_real_id":null,
"relation_parent_id":null,
"relation_parent_real_id":null,
"remote_ip":"193.33.131.226",
"shipping_method":"flatrate_flatrate",
"store_currency_code":"INR",
"store_name":"Main Website\nMain Website Store\n",
"x_forwarded_for":null,
"customer_note":null,
"created_at":"2018-11-28 06:01:59",
"updated_at":"2018-11-28 06:02:09",
"total_item_count":"1",
"customer_gender":"0",
"discount_tax_compensation_amount":"0.0000",
"base_discount_tax_compensation_amount":"0.0000",
"shipping_discount_tax_compensation_amount":"0.0000",
"base_shipping_discount_tax_compensation_amnt":"0.0000",
"discount_tax_compensation_invoiced":null,
"base_discount_tax_compensation_invoiced":null,
"discount_tax_compensation_refunded":null,
"base_discount_tax_compensation_refunded":null,
"shipping_incl_tax":"5.0000",
"base_shipping_incl_tax":"5.0000",
"coupon_rule_name":null,
"gift_message_id":null,
"paypal_ipn_customer_notified":"0",
"vendor_order_id":"VN100028",
"extension_attributes":{
},
"items":{"50":
{
}
}
}
},
{ "shipping_address":
{
"entity_id":"75",
"parent_id":"77",
"customer_address_id":null,
"quote_address_id":"250",
"region_id":"564",
"customer_id":null,
"fax":null,
"region":"chennai",
"postcode":"700089",
"lastname":"Gatto",
"street":"srinaarat",
"city":"chennai",
"email":"test#gmail.com",
"telephone":"9876543210",
"country_id":"IN",
"firstname":"hgd",
"address_type":"shipping",
"prefix":"address_","middlename":null,
"suffix":null,
"company":"sd",
"vat_id":null,
"vat_is_valid":null,
"vat_request_id":null,
"vat_request_date":null,
"vat_request_success":null
}
},
{ "billing_address":
{
"entity_id":"76",
"parent_id":"77",
"customer_address_id":"1",
"quote_address_id":"251",
"region_id":"564",
"customer_id":null,
"fax":null,
"region":"chennai",
"postcode":"700034",
"lastname":"Gatt0",
"street":"srinagar",
"city":"chennai",
"email":"text#gmail.com",
"telephone":"9848352205",
"country_id":"IN",
"firstname":"ridha",
"address_type":"billing",
"prefix":null,
"middlename":null,
"suffix":null,
"company":"ridha",
"vat_id":null,
"vat_is_valid":null,
"vat_request_id":null,
"vat_request_date":null,
"vat_request_success":null
}
},
{ "vendor":
{
"entity_id":"1",
"entity_type_id":"9",
"attribute_set_id":"0",
"increment_id":"000000009",
"parent_id":"0",
"created_at":"2018-10-01 11:09:32",
"updated_at":"2018-11-27 12:55:57",
"is_active":"1",
"website_id":"1",
"address":"chennai",
"city":"chennai",
"zip_code":"700087",
"customer_id":"1",
"shop_url":"cf97wksk",
"status":"approved",
"group":"general",
"public_name":"cf97wksk",
"name":"test",
"email":"test#gmail.com",
"contact_number":"9876543210",
"country_id":"IN",
"vendor_gstin":"11PGTAU12346Q",
"gender":"1",
"region_id":"564"
}
},
{ "items":
{"item_id":"50",
"order_id":"77",
"parent_item_id":null,
"quote_item_id":"208",
"store_id":"1",
"created_at":"2018-11-28 06:02:09",
"updated_at":"2018-11-28 06:02:09",
"product_id":"14",
"product_type":"simple",
"product_options":
{
"info_buyRequest":
{"uenc":"aHR0cDovLzE1OS44OS4xNjQuMTM5L2IyYi9idXkxLWtsbS1mYXNoaW9uLW1hbGwtd29tZW4tcy1mYW5jeS1jb3R0b24tc2lsay1zYXJlZS1yZWQteWVsbG93LXNrdS1zcmlwbC0yODU0Lmh0bWw,","product":"14","selected_configurable_option":"","related_product":"","qty":"1"},
"additional_options":[{"code":"vendor_name","label":"Vendor","value":"cf97wksk"}]},
"weight":null,
"is_virtual":"0",
"sku":"DKSR0000001-1",
"name":"KLM Fashion Mall Women's Fancy Cotton Silk Saree ( RED & YELLOW )",
"description":null,
"applied_rule_ids":null,
"additional_data":null,
"is_qty_decimal":"0",
"no_discount":"0",
"qty_backordered":null,
"qty_canceled":"0.0000",
"qty_invoiced":"0.0000",
"qty_ordered":"1.0000",
"qty_refunded":"0.0000",
"qty_shipped":"0.0000",
"base_cost":null,
"price":"1846.6800",
"base_price":"1846.6800",
"original_price":"1846.6800",
"base_original_price":"1846.6800",
"tax_percent":"5.0000",
"tax_amount":"294.5500",
"base_tax_amount":"294.5500",
"tax_invoiced":"0.0000",
"base_tax_invoiced":"0.0000",
"discount_percent":"0.0000",
"discount_amount":"0.0000",
"base_discount_amount":"0.0000",
"discount_invoiced":"0.0000",
"base_discount_invoiced":"0.0000",
"amount_refunded":"0.0000",
"base_amount_refunded":"0.0000",
"row_total":"1846.6800",
"base_row_total":"1846.6800",
"row_invoiced":"0.0000",
"base_row_invoiced":"0.0000",
"row_weight":"0.0000",
"base_tax_before_discount":null,
"tax_before_discount":null,
"ext_order_item_id":null,
"locked_do_invoice":null,
"locked_do_ship":null,
"price_incl_tax":"2141.2300",
"base_price_incl_tax":"2141.2300",
"row_total_incl_tax":"2141.2300",
"base_row_total_incl_tax":"2141.2300",
"discount_tax_compensation_amount":"0.0000",
"base_discount_tax_compensation_amount":"0.0000",
"discount_tax_compensation_invoiced":null,
"base_discount_tax_compensation_invoiced":null,
"discount_tax_compensation_refunded":null,
"base_discount_tax_compensation_refunded":null,
"tax_canceled":null,
"discount_tax_compensation_canceled":null,
"tax_refunded":null,
"base_tax_refunded":null,
"discount_refunded":null,
"base_discount_refunded":null,
"free_shipping":"0",
"gift_message_id":null,
"gift_message_available":"0",
"weee_tax_applied":null,
"weee_tax_applied_amount":null,
"weee_tax_applied_row_amount":null,
"weee_tax_disposition":null,
"weee_tax_row_disposition":null,
"base_weee_tax_applied_amount":null,
"base_weee_tax_applied_row_amnt":null,
"base_weee_tax_disposition":null,
"base_weee_tax_row_disposition":null,
"vendor_id":"1",
"seller_price":"1679",
"hsn":"5007",
"seller_item_total":"1678.80",
"vendor_tax":"83.94",
"vendor_row_total":"1762.74",
"cess_rate":"0.00",
"product":{},
"thumbnail":"\/1\/5\/1538546849_sku-sripl-2854_1.png",
}
}
]
Array
(
[shipping_address] => Array
(
[entity_id] => 1
[parent_id] => 4
[customer_address_id] => 1
[quote_address_id] => 4
[region_id] => 564
[customer_id] =>
[fax] =>
[region] => Telangana
[postcode] => 500049
[lastname] => Latname
[street] => Road No 23
Deepthisree Nagar Madinaguda
[city] => India
[email] => asarush#xmial.com
[telephone] => 9999999999
[country_id] => IN
[firstname] => Aakshay
[address_type] => shipping
[prefix] =>
[middlename] =>
[suffix] =>
[company] => Aarush
[vat_id] =>
[vat_is_valid] =>
[vat_request_id] =>
[vat_request_date] =>
[vat_request_success] =>
)
)
Array
(
[billing_address] => Array
(
[entity_id] => 2
[parent_id] => 4
[customer_address_id] => 1
[quote_address_id] => 10
[region_id] => 564
[customer_id] =>
[fax] =>
[region] => Telangana
[postcode] => 500076
[lastname] => Gupta
[street] => Road No 23
Golkonda
[city] => Hyderabad
[email] => fradnsss#gmail.com
[telephone] => 9999999999
[country_id] => IN
[firstname] => Firsthan
[address_type] => billing
[prefix] =>
[middlename] =>
[suffix] =>
[company] => Firstname
[vat_id] =>
[vat_is_valid] =>
[vat_request_id] =>
[vat_request_date] =>
[vat_request_success] =>
)
)
Array
(
[vendor] => Array
(
[entity_id] => 1
[entity_type_id] => 9
[attribute_set_id] => 0
[increment_id] => 000000009
[parent_id] => 0
[created_at] => 2018-10-01 11:09:32
[updated_at] => 2018-11-27 12:55:57
[is_active] => 1
[website_id] => 1
[address] => hyyderabad
[city] => hyderabad
[zip_code] => 500055
[customer_id] => 1
[shop_url] => cf97wksk
[status] => approved
[group] => general
[public_name] => cf97wksk
[name] => Firstname Lastname
[email] => testemail#gmail.com
[contact_number] => 9999999999
[country_id] => IN
[vendor_gstin] => 11PGTAU12346Q
[gender] => 1
[region_id] => 564
)
)
In my controller sending the data to view as follows:
$result = json_decode($result_json,true);
return view('orders.view',compact('result'));
I need to display shipping address and billing address in my view blade. How can i do that.
I expect the out put like below
Shipping Address
Firstname Lastname
Hyderabad
Pin Code : 500072
Contact Number : 999888777
In your controller
return view('view-name', [
'shipping_address' => $shipping_address
// ...
]);
In your view-name.blade.php file:
<input id="shipping_address" type="hidden" value="#json($shipping_address)">
In a Javascript file:
let shipping_address= $('#shipping_address').val();
console.log(shipping_address)
See console log in browser debug and you can do anything.
you cannot use $result->shipping_address because as per your output that is array.
Try below one.
echo $result[1]['shipping_address']['firstname'] . ' ' . $result[1]['shipping_address']['lastname'] . ' ' . $result[1]['shipping_address']['city'] . 'Pin code: ' . $result[1]['shipping_address']['postcode'] . ' ' . 'Contact Number : ' . $result[1]['shipping_address']['telephone'];
or
{{$result[1]['shipping_address']['firstname']}} {{$result[1]['shipping_address']['lastname']}} {{$result[1]['shipping_address']['city']}} Pin code: {{$result[1]['shipping_address']['postcode']}} Contact Number : {{$result[1]['shipping_address']['telephone']}}
I have just provided option to print value, you may required some validation for variables.
Thank you.