darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.34k stars 443 forks source link

Too few arguments to function Darryldecode\Cart\ItemCollection::__construct() #197

Open MohammaddReza opened 5 years ago

MohammaddReza commented 5 years ago

in laravel 6.1 i have this error: its working on L5.8 Too few arguments to function Darryldecode\Cart\ItemCollection::__construct(), 1 passed in vendor\laravel\framework\src\Illuminate\Support\Collection.php on line 640 and exactly 2 expected

zheldibayev commented 5 years ago

Without seeing your code we can not help you

yudijohn commented 4 years ago

Got same error when i try to call toArray() function on cart content

$carts = Cart::session( Auth::id() )->getContent();
return $carts->toArray();

when i call toJson() function, i works fine, but when i call toArray() function, it showed the thread error

Too few arguments to function Darryldecode\Cart\ItemCollection::__construct(), 1 passed in vendor\laravel\framework\src\Illuminate\Support\Collection.php on line 640 and exactly 2 expected

I'm using PHP 7.3 and Laravel 6.4

FOR SHORT FIX, i use toJson() function

json_decode( $carts->toJson(), true );

But with that way, i can't call the CartCollenction functions like getPriceSum()

juddfranklin1 commented 4 years ago

I am having the same issue. I am using PHP 7.3 and Laravel 6.9.

It appears that somewhere in my code, nothing is being passed as $config in the ItemCollection __construct magic method. When I change that method to default $config to an empty array, my code instantly works.

Is there any reason there shouldn't be a default value on ItemCollection?

juddfranklin1 commented 4 years ago

Just like @yudijohn, the issue for me is in the ->toArray() method.

Just to give you a bit more context, my code calls the package like this:

    public function store(AddToCartRequest $request)
    {
        $product = Product::findOrFail($request->id);

        $options = $request->except('_token', 'id');
        $options['main_image'] = $product->main_image;

        // create new cart item
        $cartItem = $this->cartRepository
                        ->addToCart($product, $request->quantity, $options)
                        ->get($product->id);

        // at this point $cartItem is an instance of Darryldecode\Cart\ItemCollection

        $cartItem = $cartItem->toArray();

        // The error occurs within the toArray() call.

        $cartItem['deleted'] = false;

        return response()->json($cartItem);
    }
juddfranklin1 commented 4 years ago

My colleague resolved the issue by using ->all() instead of ->toArray().