简体   繁体   English

Laravel 5-从集合中获取价值并传递给视图

[英]Laravel 5 - Getting values from collection and passing to view

I am building an order management system in Laravel 5.3. 我正在Laravel 5.3中构建订单管理系统。 I am storing the orders in two different tables - orders, order_content. 我将订单存储在两个不同的表中-order,order_content。 The first one contains order's main data, the other - content. 第一个包含订单的主要数据,另一个包含内容。 I am using Crinsane's Shopping Cart package for managing order contents. 我正在使用Crinsane的Shopping Cart软件包来管理订单内容。 The order contents table fields: identifier, instance, content. 订单内容表字段:标识符,实例,内容。 Content field contains collection with the specific order content, which is searialized. 内容字段包含具有特定订单内容的集合,该集合已进行了序列化。

My problem is following - i need to get all order contents (product name, code, quantity) in specific date. 我的问题是-我需要在特定日期获取所有订单内容(产品名称,代码,数量)。

This is how far I've gotten: 这是我走了多远:

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

    //get all orders by date, which are confirmed
    $orders = Pasutijums::where('pasutijums_uz', '=', $date)
        ->where('ir_apstiprinats', '=', 1)->get();

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

        foreach($order_contents as $order)
        {
            // unserialize the content field
            $collection = unserialize($order->content);
        }

    }

    $items = $collection->all();

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

}

In the view I try to loop trough the order contents, but only one product is showing... 在视图中,我尝试遍历订单内容,但仅显示一种产品...

 @foreach($items as $order)


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


@endforeach

I need to unserialize each field, save it in collection and pass it to view so i can loop trough them and show them in a table. 我需要反序列化每个字段,将其保存在集合中并传递给视图,以便我可以遍历它们并在表中显示它们。

UPDATE: Walter Cejas answer worked for me, but now i need to access the collection's content in my view... This is what I get when dd($collection) in controller: 更新:Walter Cejas答案对我有用,但是现在我需要在我的视图中访问集合的内容...这是在控制器中使用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
    }
  ]
 }
]

} }

Thats because you're overriding the collection with the last item of the iteration 那是因为您用迭代的最后一项覆盖了集合

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

//get all orders by date, which are confirmed
$orders = Pasutijums::where('pasutijums_uz', '=', $date)
    ->where('ir_apstiprinats', '=', 1)->get();
$collection = collect();
foreach($orders as $order)
{
    // get order's content
    $order_contents = DB::table('order_content')->where('instance', '=', $order- 
         >pasut_num)->get();


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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM