Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel中使用表中的对象数组_Php_Html_Arrays_Laravel_Html Table - Fatal编程技术网

Php 在laravel中使用表中的对象数组

Php 在laravel中使用表中的对象数组,php,html,arrays,laravel,html-table,Php,Html,Arrays,Laravel,Html Table,嗯,我是拉雷维尔的新手,我不知道我是否做得对。 所以这里是我的问题:我有一个控制器,它获取id并给出一个包含数组的对象。 我只需要数组中的对象,我可以这样得到它们: $services = Service::findOrFail($id); $service = $services->order['orders']; 但当我发送$service到view时,我不知道如何使用在foreach表中设置它们 这是我的控制器: public function watch($id)

嗯,我是拉雷维尔的新手,我不知道我是否做得对。 所以这里是我的问题:我有一个控制器,它获取id并给出一个包含数组的对象。 我只需要数组中的对象,我可以这样得到它们:

$services = Service::findOrFail($id);
    $service = $services->order['orders'];
但当我发送$service到view时,我不知道如何使用在foreach表中设置它们

这是我的控制器:

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

       return view('service.watch',['service'=> $service]);
}
通常,当我从一个集合中获取所有文档并将它们发送到视图时,这很容易,因为这两个文档的结构相同,但工作方式不同

这是当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"
             }
   }
在我看来,我是这样做的:

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

请帮我编码这个。谢谢

正如您在var_dump结果中看到的,您的结果数据类型是array,而不是object。
因此,您必须通过索引$value['customerPhone']

使用数组索引,如
$value['customerPhone']
@zahidhasanemon,谢谢它的工作。但是你能解释一下为什么$value->customerPhone不起作用吗?是的,谢谢,它起作用了,但我有一个问题。当我们从集合中获取所有文档时,它是相同的。不是吗?