Object of class Illuminate\Support\Collection could not be converted to int php beginner

1000 views Asked by At

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

There are 1 answers

5
YaatSuka On

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