laravel getting error on empty request

I get error below while trying to add item to my cart:

Darryldecode \ Cart \ Exceptions \ InvalidItemException validation.numeric

The error comes from this part of my code:

$customAttributes = [];
      if(!empty($request->attr)){
          foreach($request->attr as $sub) {
          // find the suboption
              $sub = Suboption::find($sub);
              if (!empty($sub->id)) {
                  $itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
                      'name' => $sub->title,
                      'value' => $sub->price,
                      'type' => 'additional',
                      'target' => 'item',
                  ));

                  array_push($customAttributes, $itemCondition1);
              }
          }
      }

and it take place in here:

Cart::add(array(
        'id' => $product->id,
        'name' => $product->title,
        'price' => $price,
        'quantity' => $request->input('quantity'),
        'attributes' => $weightArray,
        'conditions' => $customAttributes,  //here
));

The $customAttributes code supposed to get data IF product does have those information And user chose any of it and suppose to ignore if product doesn't have any of those info or user didn't chose any of it.

Issue is

The code expect data no matter what, product does have that info or not, user selected any or not, even if user select that data still i get error above.

Demo

https://imgur.com/a/KHJqp

any idea why is that?

UPDATE

I figured my issue comes from 'price' => $price, in my add method where i get my $price like:

$price = $product->discounts;
      if($price->count() > 0 ) {
        foreach($discounts as $disc){
          if($disc->value_to >= $mytime) {
            $price = $product->price - $disc->amount;
          }
        }
      }else{
        $price = $product->price;
      }

this part supposed to get product price if there is no discount, and get discounted price if there is.

How I get to this line of code? here is it

1 answer

  • answered 2018-02-13 01:40 mafortis

    SOLVED

    I used hidden field and got my data from front-end instead of controller method.