How to use @foreach Loop to check condition with if else statement

Published 4 months ago by dreamxyz

$id_supplier = Product::whereIn('id_product', $check)->groupBy('id_supplier')->pluck('id_supplier');

How can i loop this supplier to check with some condition during looping?

I tried this but it only loop the first item.

   foreach($id_supplier as $key => $val)
Best Answer (As Selected By dreamxyz)
mushood

the dd() function will only dump one result in each case.

If it is inside the foreach, it will dump the first result only. If it is outside the foreach, it will dump the last result.

in order to have both, try this:

foreach($id_supplier as $id ) {
        $result = DB::table('cart_items')
        ->leftJoin('products', 'cart_items.id_product', '=', 'products.id_product')
        ->leftJoin('suppliers', 'products.id_supplier', '=', 'suppliers.id_supplier')
            ->select(DB::raw('sum(cart_items.quantity*cart_items.unit_price) AS total_sales'))
        ->where('cart_items.id_cart', $id_cart)
        ->where('products.id_supplier', $id)
        ->get();

dump($result);
}
die();

also from your code, check that $id_cart is defined.

mushood

Hello

The foreach should actually work. I suggest you dd($id_supplier) to see what is actually being retrieved.

dreamxyz

I have 2 supplier which is with id - 2 and id - 3, but it only show id - 2. I tried to see what is actually being retrieved, i have no idea about it. i have to check some condition inside controller, but i unable to do that.

Mittensoff

Do a dd($id_suplier) somewhere in your code and paste the results here.

dreamxyz

yes i did it, it shows 2 id, but when put inside loop, it only shows the first id.

Mittensoff

Just do a

foreach($id_suplier as $id) {

}

Seems like pluck with that one argument returns only an indexed array.

dreamxyz

Still the same...

dreamxyz

I want to get the total sales and do comparison, anyhow i only able to show 1 record of supplier, i will not able to compare the second supplier.

foreach($id_supplier as $id ) {
        $result = DB::table('cart_items')
        ->leftJoin('products', 'cart_items.id_product', '=', 'products.id_product')
        ->leftJoin('suppliers', 'products.id_supplier', '=', 'suppliers.id_supplier')
            ->select(DB::raw('sum(cart_items.quantity*cart_items.unit_price) AS total_sales'))
        ->where('cart_items.id_cart', $id_cart)
        ->where('products.id_supplier', $id)
        ->get();

        dd($result);
Mittensoff

What about now?

foreach($id_supplier as $id ) {
        $result = DB::table('cart_items')
        ->leftJoin('products', 'cart_items.id_product', '=', 'products.id_product')
        ->leftJoin('suppliers', 'products.id_supplier', '=', 'suppliers.id_supplier')
            ->select(DB::raw('sum(cart_items.quantity*cart_items.unit_price) AS total_sales'))
        ->where('cart_items.id_cart', $id_cart)
        ->where('products.id_supplier', $id)
        ->get();
}
dd($result);
dreamxyz

This is not the problem..The main problem is I only able to loop 1 time when their is 2 id retrieved.

Mittensoff

The only problem I see here is the dd() function. If you remove it it should all work. Otherwise, I don't really know.

dreamxyz

I use dd() for tracking purpose only.

shakti

dd() work as print_r();exit();

so while you have place dd() inside the loop the loop will run only one time and after dd() been called it get exist

hope it might help

mushood

the dd() function will only dump one result in each case.

If it is inside the foreach, it will dump the first result only. If it is outside the foreach, it will dump the last result.

in order to have both, try this:

foreach($id_supplier as $id ) {
        $result = DB::table('cart_items')
        ->leftJoin('products', 'cart_items.id_product', '=', 'products.id_product')
        ->leftJoin('suppliers', 'products.id_supplier', '=', 'suppliers.id_supplier')
            ->select(DB::raw('sum(cart_items.quantity*cart_items.unit_price) AS total_sales'))
        ->where('cart_items.id_cart', $id_cart)
        ->where('products.id_supplier', $id)
        ->get();

dump($result);
}
die();

also from your code, check that $id_cart is defined.

mushood

it all depends.

You could just retrieve the value and use a switch() or if() elseif() else

shakti

do something like

foreach($id_supplier as $id ) {
        $result = DB::table('cart_items')
        ->leftJoin('products', 'cart_items.id_product', '=', 'products.id_product')
        ->leftJoin('suppliers', 'products.id_supplier', '=', 'suppliers.id_supplier')
            ->select(DB::raw('sum(cart_items.quantity*cart_items.unit_price) AS total_sales'))
        ->where('cart_items.id_cart', $id_cart)
        ->where('products.id_supplier', $id)
        ->get();
echo "<pre>";
print_r($result);
echo "</pre>";
}
die();

hope it might help you and its the solution which you wanted ....if not then can you please make us clear what actually you want

Please sign in or create an account to participate in this conversation.