douou0977 2017-10-05 02:54
浏览 579

类Illuminate \ Support \ Collection的对象无法转换为int php初学者

please give me some suggestions on how my code will work properly. I am having a loop that gives me an id and quantity to update every loop into my database.

but i got an error when i pass it to my private function and try to execute it. I am just a beginner in PHP/Laravel. Here is my full code:

public function updateProduct(Request $request) {
    $ids = array();
        foreach ($request->products as $ship_id) {
            $product_id = DB::table('shipping_products')->select('product_id','quantity')
            ->where(['shipping_products.shipping_id'=>$ship_id])
            ->get();
            array_push($ids,$product_id);
        }

        foreach ($ids as $value) {
            foreach ($value as $update) {
                $this->updateProductQty($update->product_id,$update->quantity);
            }
        }
    }

    private function updateProductQty($product_id, $quantity_order) {
        $qty = DB::table('products')->select('quantity')
            ->where(['products.product_id'=>$product_id])
            ->get();
        $updateTotalQty = $qty - $quantity_order;
        DB::table('products')
            ->where('product_id', $product_id)
            ->update(array(           
            'quantity' => $updateTotalQty
        ));

    }

i got an error in this lines:

$this->updateProductQty($update->product_id,$update->quantity);

it says error message:

Object of class Illuminate\Support\Collection could not be converted to int
  • 写回答

1条回答 默认 最新

  • dongnue2071 2017-10-05 03:26
    关注

    Try to do that to check if the data is what you're looking for:

    foreach ($ids as $value) {
            foreach ($value as $update) {
                //$this->updateProductQty($update->product_id,$update->quantity);
                var_dump($update);
                var_dump($update->product_id);
                var_dump($update->quantity);
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装