Home > Mobile >  Laravel foreach not working into my product multiiple discount model?
Laravel foreach not working into my product multiiple discount model?

Time:11-30

I have build discount system based on product and user id, I have almost done everything i just got error in my laravel model bellow my code: public static function discount($p_id) {

    $discount = Discount::where('user_id', Auth::user()->id )->first();
    
    $p = Product::where('id', $p_id)->first();

    $multidiscount =  MultipleDiscount::where('user_id','like','%'.trim(Auth::user()->id).'%')->with('typeItems')->first();

    $orderdetail = MultiDiscountType::where('multidiscount_id',$multidiscount->id)->pluck('value');        
    if( !is_null($multidiscount) || is_null($discount) ){
        foreach ($multidiscount->typeItems as $key => $mdiscount) {
            # code...
            $discprice = explode(',', $orderdetail);
            if( $discount->value <= $mdiscount->value ){
                if( $mdiscount->type == 'percentage' ){
                    $p_val = $p->price / 100 * $mdiscount->value ;
                    $p_price = $p->price - $p_val;
                }
            }else{
                if( $discount->type == 'percentage' ){
                    $p_val = $p->price / 100 * $discount->value;
                    $p_price = $p->price - $p_val;
                }
                else {
                    $p_val = $p->price - $discount->value;
                    $p_price = $p_val;
                }
            }
        }
    }
    else {
        $p_price = $p->price;
    }

    return $p_price;

}

I got nice result in my product blade view file but issue with in my multidiscount table have two rows but i get only last row value. Whats is the wrong i dont know bellow multiplediscount table structure:

Multi Discount Table:
|id|     name   |status |user_id|
|1 |Black Friday| active| 2, 3  |

I used repeater field for multiple discount based on product and user.

Multi Discount Table Item
    |id|  product it  |value|discount_id|
    |1 |   3          |  25 |    1      |
    |2 |   2          |  20 |    1      |

CodePudding user response:

Use get() instead of first() in your MultipleDiscount module to fetch all records:

$multidiscount =  MultipleDiscount::where('user_id','like','%'.trim(Auth::user()->id).'%')->with('typeItems')->get();

Although I'm not sure why you are fetching the discounts for a specific user with the LIKE clause. For example, if you have a user with the ID of 10, this will return values for the users with the ID that somehow contains 10 like 1101, 1210212, ... and so on.

EDIT:

foreach ($multidiscount as $key => $mdiscount) {
    # code...
    $discprice = explode(',', $orderdetail);
    if( $discount->value <= $mdiscount->typeItems->value ){
        if( $mdiscount->typeItems->type == 'percentage' ){
            $p_val = $p->price / 100 * $mdiscount->typeItems->value ;
            $p_price = $p->price - $p_val;
        }
    }else{
        if( $discount->type == 'percentage' ){
            $p_val = $p->price / 100 * $discount->value;
            $p_price = $p->price - $p_val;
        }
        else {
            $p_val = $p->price - $discount->value;
            $p_price = $p_val;
        }
    }
}
  • Related