jeudi 6 décembre 2018

Laravel 5 - Getting values from collection and passing to view

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">  </td>
            <td>  </td>
            <td>  </td>
        </tr>


@endforeach

This is what I get when I dd($collection) in the second foreach:

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

] }

I also tried echo'ing each $order->content and it worked, i got all the contents. But 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire