Laravel 5-在视图中访问集合值

时间:2018-12-06 19:44:59

标签: php laravel-5 laravel-collection

我正在Laravel 5.3中构建订单管理系统。我将订单存储在两个不同的表中-order,order_content。第一个包含订单的主要数据,另一个包含内容。我正在使用Crinsane的Shopping Cart软件包来管理订单内容。订单内容表字段:标识符,实例,内容。 “内容”字段包含具有特定订单内容的集合,该集合已进行了序列化。

我将每个订单的内容添加到集合中,然后将该集合传递给视图。我的问题是我不知道如何在视图中访问集合的项目。 控制器:

 public function showByDate()
{
    $datetime_today = new DateTime('now');
    $date = $datetime_today->format('Y-m-d');

    $collection = collect();

    foreach($orders as $order)
    {
        $order_contents = DB::table('order_content')->where('instance', '=', $order->pasut_num)->get();

        foreach($order_contents as $content)
        {
            $collection->push(unserialize($content->content));
        }

    }


    $items = $collection->all();

    return view('admin.kopsavilkumi.pec-datuma')
        ->with('orders', $orders)
        ->with('date', $date)
        ->with('items', $items)
        ;



}

当我dd($ collection)我得到:

Collection {#292 ▼
    #items: array:2 [▼
    0 => Collection {#307 ▼
        #items: array:2 [▼
        "da951a56dc871db7b48a41f7d14b007b" => CartItem {#308 ▼
            +rowId: "da951a56dc871db7b48a41f7d14b007b"
            +id: "22"
            +qty: "1"
            +name: "Preces nosaukums 1"
            +price: 7.5
            +options: CartItemOptions {#309 ▶}
                -associatedModel: null
                -taxRate: 0
                +"priceTax": 7.5
    }
    "138ba08bdbf1ac8f17e9e6f257af1b88" => CartItem {#310 ▼
                +rowId: "138ba08bdbf1ac8f17e9e6f257af1b88"
                +id: "123"
                +qty: "1"
                +name: "Preces nosaukums 2"
                +price: 1.8
                +options: CartItemOptions {#311 ▶}
                    -associatedModel: null
                    -taxRate: 0
    }
  ]
}
1 => Collection {#306 ▼
                #items: array:1 [▼
                "da951a56dc871db7b48a41f7d14b007b" => CartItem {#312 ▼
                    +rowId: "da951a56dc871db7b48a41f7d14b007b"
                    +id: "22"
                    +qty: "1"
                    +name: "Preces nosaukums 3"
                    +price: 7.5
                    +options: CartItemOptions {#314 ▶}
                        -associatedModel: null
                        -taxRate: 0
    }
  ]
 }
]
}

查看:

 @foreach($items as $item)

       @foreach($item as $order)

           <tr>
               <td class="text-primary"> {{ $order->options->code }} </td>
               <td> {{ $order->items->name }} </td>
               <td> {{ $order->items->qty }} </td>
           </tr>


       @endforeach

@endforeach

0 个答案:

没有答案