【问题标题】:Laravel 5 - Getting values from collection and passing to viewLaravel 5 - 从集合中获取值并传递给视图
【发布时间】:2018-12-07 02:25:51
【问题描述】:

我正在 Laravel 5.3 中构建一个订单管理系统。 我将订单存储在两个不同的表中 - 订单、订单内容。 第一个包含订单的主要数据,另一个 - 内容。我正在使用 Crinsane 的购物车包来管理订单内容。订单内容表字段:标识符、实例、内容。内容字段包含具有特定订单内容的集合,已序列化。

我的问题如下 - 我需要在特定日期获取所有订单内容(产品名称、代码、数量)。

这是我已经走了多远:

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)
        ;

}

在视图中,我尝试遍历订单内容,但只显示一种产品...

 @foreach($items as $order)


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


@endforeach

我需要反序列化每个字段,将其保存在集合中并将其传递给视图,以便我可以循环遍历它们并将它们显示在表格中。

更新: 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
    }
  ]
 }
]

}

【问题讨论】:

  • 您只得到 1 项,因为您在循环中覆盖了 $collection
  • 感谢您的回答。是的,我想到了这一点,并尝试以不同的方式将它们保存在另一个中,但没有奏效。

标签: php laravel-5 foreach laravel-collection


【解决方案1】:

那是因为你用迭代的最后一项覆盖了集合

$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));
    }

}

【讨论】:

  • 谢谢,这成功了!但是我现在如何在我的视图中访问它们?更新了控制器中 dd() 集合时当前输出的问题。
  • @unity115 如果答案有帮助,请接受并投票。如果您有新问题,请提出新问题。
  • 太棒了!如果是,您可以将我的问题标记为已回答!如果您使用刀片,请创建一个 @foreach,也许您还想将一组项目附加到每个订单,在实际状态下,您将获得同一集合中的所有项目
猜你喜欢
  • 2017-05-21
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
相关资源
最近更新 更多