0

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
Jc John
  • 1,799
  • 2
  • 34
  • 69
  • What are the declared type of `products` and `quantity` ? I think that `products` is a `CollectionType` – YaatSuka Oct 05 '17 at 03:02
  • @YaatSuka thanks for your fast reply. Here is it {#300 ▼ +"product_id": 1 +"quantity": 1 } when i dd($update) inside the foreach – Jc John Oct 05 '17 at 03:04
  • What's the value of `$updateTotalQty` ? – YaatSuka Oct 05 '17 at 03:06
  • private function updateProductQty($product_id, $quantity_order) its a function that what i want is just to update my data from database everytime the foreach loop got its data – Jc John Oct 05 '17 at 03:07
  • Sorry, I talked about the variable at this line: `$updateTotalQty = $qty - $quantity_order;` – YaatSuka Oct 05 '17 at 03:09
  • @YaatSuka ops. sorry sir. its a variable where $qty is the quantity of my database from products table and i want it to be deducted by the ordered quantity. and store it in $updateTotalQty data and after storing that is my new quantity that i will update in my database. just like the function that i've posted. after deducting, i want to update it. thats what my function wants – Jc John Oct 05 '17 at 03:16
  • ok but can you `var_dump()` the value, to be sure that the problem doesn't comes from that variable or its type? – YaatSuka Oct 05 '17 at 03:18
  • @YaatSuka i tried this:$updateTotalQty = $qty - $quantity_order; var_dump($updateTotalQty); but i cant see the value because i am getting the error whenever i will activate the private function – Jc John Oct 05 '17 at 03:21

1 Answers1

1

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);
        }
    }
YaatSuka
  • 231
  • 1
  • 9
  • yes it is? the $update->product_id gives me what should i want and also the $update->quantity. i think its because they are objects. and my $updateTotalQty is an int? – Jc John Oct 05 '17 at 03:28
  • object(stdClass)#300 (2) { ["product_id"]=> int(1) ["quantity"]=> int(1) } int(1) int(1) object(stdClass)#299 (2) { ["product_id"]=> int(2) ["quantity"]=> int(1) } int(2) int(1) object(stdClass)#298 (2) { ["product_id"]=> int(1) ["quantity"]=> int(1) } int(1) int(1) – Jc John Oct 05 '17 at 03:32
  • var_dump($update->product_id); int(1) int(2) int(1) var_dump($update->quantity); int(1) int(1) int(1) – Jc John Oct 05 '17 at 03:39
  • tried this but nothing happens // $updateTotalQty = $qty->quantity - $quantity_order; – Jc John Oct 05 '17 at 03:44
  • Please, put that on the first line of your private function (before the query): `var_dump($product_id);die;` and give me the result. I think that one variable is not the type you're looking for: it could be an array or a collection of integers – YaatSuka Oct 05 '17 at 04:03