alireza
alireza

Reputation: 62

using array of objects in table in laravel

well i am new to laravel and i do not know if i am doing this right or what. so here is my problem : i have controller that gets id and gives an object that has array in it . and i just need the objects in that array which i can get them like this :

$services = Service::findOrFail($id);
    $service = $services->order['orders'];

but when i send $service to view i do not know how to use set them in table with foreach.

this is my controller :

    public function watch($id){
        $services = Service::findOrFail($id);
        $service = $services->order['orders'];

       return view('service.watch',['service'=> $service]);
}

it usually easy when i get all docs from a collection and send them to view and both of them are the same in structure but it does not work like this .

this is when i var_dump $service :

 array(2) {
  [0]=>
  array(9) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#1221 (1) {
      ["oid"]=>
      string(24) "5f42734c00c6ed74e48624e5"
    }
    ["stat"]=>
    string(5) "false"
    ["customerPhone"]=>
    string(1) "5"
    ["orderQuantity"]=>
    string(1) "5"
    ["orderCost"]=>
    string(1) "5"
    ["orderId"]=>
    float(139907061720)
    ["orderTag"]=>
    string(1) "2"
    }
  [1]=>
  array(9) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#1222 (1) {
      ["oid"]=>
      string(24) "5f42734c00c6ed74e48624e6"
    }
    ["stat"]=>
    string(5) "false"
    ["customerPhone"]=>
    string(1) "9"
    ["orderQuantity"]=>
    string(1) "9"
    ["orderCost"]=>
    string(1) "9"
    ["orderId"]=>
    float(139907061725)
    ["orderTag"]=>
    string(1) "9"
             }
   }

in my view i did this :

@foreach($service as $key=>$value)
   <td>{{ $value->customerPhone }}</td>
   <td>{{ $value->orderQuantity }}</td>
   <td>{{ $value->orderCost}}</td>
   <td>{{ $value->orderTag}}</td>
   @endforeach

please help me to code this . Thank You

Upvotes: 1

Views: 435

Answers (1)

codelearner
codelearner

Reputation: 26

As you can see in your var_dump result, your result data type is array, not object. So you must access by index $value['customerPhone']

Upvotes: 1

Related Questions