1

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

2
  • use array index like $value['customerPhone']. Commented Aug 23, 2020 at 15:22
  • @zahidhasanemon thanks it worked . but can u please explain to me why $value->customerPhone doesn't work ? Commented Aug 23, 2020 at 15:26

1 Answer 1

1

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']

Sign up to request clarification or add additional context in comments.

1 Comment

yes thank you it worked but i have a question about that . when we get all doc from collection it is the same . isn't it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.