douyan3478 2018-12-06 18:05
浏览 69
已采纳

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

I am building an order management system in Laravel 5.3. I am storing the orders in two different tables - orders, 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. 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:

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

}

  • 写回答

1条回答 默认 最新

  • drpjdfj618393 2018-12-06 18:15
    关注

    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));
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献