为了账号安全,请及时绑定邮箱和手机立即绑定

在订单控制器 PHP Laravel 5.7 中增加产品数量

在订单控制器 PHP Laravel 5.7 中增加产品数量

PHP
翻阅古今 2024-01-19 20:47:25
我可以,我们可以在 Laravel 项目上使用增量,我打算在商店中使用它并更新控制器吗?我尝试在控制器中存储数据的同时进行更新,处理产品、订单和订单详细信息模型。这是我到目前为止所得到的:应用\订单控制器public function store(Request $request)    {        $order = Order::create($request->all());                $products = $request->input('products', []);        $quantities = $request->input('quantities', []);        for ($product=0; $product < count($products); $product++) {            if ($products[$product] != '') {                $order->products()->attach($products[$product], ['quantity' => $quantities[$product]]);            Product::where('id', $products[$products])->increment('qty',$quantities[$product]);             }        }            return redirect()->route('orders.index');    }所以这行就是问题所在。我们可以在 store 函数上做这样的增量吗?产品::where('id', $products[$products])->increment('qty',$quantities[$product]);我的意思是,我应该在收到订单后增加产品的数量,对吧?我没有成功,而是收到一条错误消息,如下所示:ErrorException (E_WARNING) 非法偏移类型我被困住了。请帮忙。
查看完整描述

1 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

您正在尝试使用自己作为索引来访问数组。应该是$products[$product],不是$products[$products]


public function store(Request $request)

{

    $order = Order::create($request->all());

    

    $products = $request->input('products', []);

    $quantities = $request->input('quantities', []);


    for ($product = 0; $product < count($products); $product++)

    {

        if ($products[$product] != '')

        {

            $order->products()->attach($products[$product], ['quantity' => $quantities[$product]]);


            // $products[$product], without the 's'

            Product::where('id', $products[$product])->increment('qty',$quantities[$product]); 

        }

    }


    return redirect()->route('orders.index');

}


查看完整回答
反对 回复 2024-01-19
  • 1 回答
  • 0 关注
  • 29 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信