Get data from array of array object in laravel

2019-09-12 07:05发布

问题:

I have this data:

[{"order_id":37,"order_code":"Order00003","user_id":1,"full_name":"Duc.VUong","phone":"01203388870","address":"445\/33b b\u00ecnh \u0111\u00f4ng","recieve_address":"445\/33b b\u00ecnh \u0111\u00f4ng","total_product":2,"total_price":1450000,"status":0,"created_at":"2017-04-12 17:01:38","updated_at":"2017-04-12 17:01:38","detail_orders":[{"id":29,"order_id":37,"order_code":"Order00003","product_id":1,"price":800000,"quantity":1,"total_amount":800000,"created_at":"2017-04-12 17:01:38","updated_at":"2017-04-12 17:01:38","product":[{"product_id":1,"product_name":"H\u01b0\u01a1ng N\u1eafng","product_type_id":1,"price":800000,"description":"H\u01b0\u01a1ng n\u1eafng g\u1ed3m c\u00f3:hoa h\u1ed3ng da t\u1ea1o n\u00ean gam m\u00e0u nh\u1eb9 nh\u00e0ng nh\u01b0ng kh\u00f4ng k\u00e9m ph\u1ea7n thu h\u00fat. Th\u00edch h\u1ee3p \u0111\u1ec3 t\u1eb7ng ch\u00fac m\u1eebng, sinh nh\u1eadt, k\u1ec9 ni\u1ec7m,...","product_image":"2385_huong-nang.jpg","addition_information":"H\u01b0\u01a1ng n\u1eafng g\u1ed3m c\u00f3:\r\n\r\n- Hoa h\u1ed3ng da\r\n\r\n- Hoa t\u00fa c\u1ea7u\r\n\r\n- Hoa baby\r\n\r\n- Hoa l\u00e1 ph\u1ee5 kh\u00e1c","status":1,"created_at":null,"updated_at":null}]},{"id":30,"order_id":37,"order_code":"Order00003","product_id":2,"price":650000,"quantity":1,"total_amount":650000,"created_at":"2017-04-12 17:01:38","updated_at":"2017-04-12 17:01:38","product":[{"product_id":2,"product_name":"My everything","product_type_id":1,"price":650000,"description":"\u201cYou are my everything You are enough and the best for me\u201d L\u1eddi b\u00e0i h\u00e1t nh\u01b0 \u0111\u00e3 n\u00f3i l\u00ean t\u00e2m t\u01b0 c\u1ee7a nh\u1eefng tr\u00e1i tim \u0111ang y\u00eau, ng\u1ecdt ng\u00e0o v\u00e0 l\u00e3ng m\u1ea1n v\u00f4 c\u00f9ng. B\u00f3 hoa \u201cMy everything\u201d c\u0169ng nh\u01b0 th\u1ebf, \u0111\u00e1ng y\u00eau v\u00e0 t\u01b0\u01a1i s\u00e1ng. Th\u00edch h\u1ee3p t\u1eb7ng sinh nh\u1eadt, k\u1ec9 ni\u1ec7m, v.v\u2026","product_image":"2511_mat-ngot.jpg","addition_information":"B\u00f3 hoa \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf t\u1eeb:\r\n\r\n-         25 hoa h\u1ed3ng \u0111\u1ecf\r\n\r\n-         Hoa baby tr\u1eafng\r\n\r\n-         Hoa l\u00e1 ph\u1ee5 kh\u00e1c","status":0,"created_at":null,"updated_at":null}]}]}]

My code

@extends('templates.master')
@section('content')
<div class="container">
    @foreach ($orders as $order)
    @php
    echo "
    <pre>";
    $arr=array();
    $arr= $order->detail_orders;

    @foreach ($arr as $item)
    echo $item->id;
    @endforeach
    echo "</pre>";
    @endphp
    @endforeach 
    @stop
</div>

when i echo $arr, it have data, but to second foreach, i get message: FatalErrorException in 04212fe3982c22912d313c7bbaa7970ed5d682e8.php line 10: syntax error, unexpected '<' So, how can i get the id in this for show it in view?

回答1:

If you want to convert array object data in to array. This is very simple. You first get all the records.Something like that:

$orders = DB::table('orders')->get(); //get all records form table
$orders = json_decode(json_encode($orders),true);// this will convert data into array
echo "<pre>"; print_r($orders); die; // now echo

and after that you can use foreach in blade file