Skip to content
Advertisement

ArgumentCountError Too few arguments to function AppHttpControllersShopCartController::addToCart()

So this function is supposed to add the product into a cart, but i’ve been getting the error

Too few arguments to function AppHttpControllersShopCartController::addToCart(), 0 passed in C:xampphtdocsnerdSvendorlaravelframeworksrcIlluminateRoutingController.php on line 54 and exactly 1 expected

I tried changing key words here and there on my controller, but nothing seems to do it. This is the the controller:

<?php

namespace AppHttpControllersShop;

use AppHttpControllersController;
use IlluminateHttpRequest;
Use AppModelsProduct;
Use AppModelsOrder;

class CartController extends Controller {
    public function placeOrder() {
        if(session('id')){
            if(Cart::count()){
            AppModelsOrder::store();
            return redirect('shop')->with('status', 'Thank you for buying!');
            }
                return redirect('cart');
            }
            session(['place-order-process' => true]);
            return redirect('login')->with('status', 'To complete your order, please log in. Not a member yet? <a href="'.url('signup') . '"> Join the club! </a>');
        }
    
    public function deleteCart() {
        Cart::destroy();
        return redirect('shop')->with('status', 'The cart is now empty.');
    }
    
    public function deleteItem($rowId) {
        Cart::remove($rowId);
        return redirect('cart')->with('status', 'The item was deleted.');
    }
    
    public function updateCart(Request $request){
        Cart::update($request->rowId, $request->quantity);
        $data = [
            'cart_count' =>Cart::count(),
            'cart_total' => Cart::total(),
            'product_total' => Cart::get($request->rowId)->total(),
        ];
        return json_encode($data);
    }
    
    public function displayCart() {
        Cart::setGlobalTax(0);
        $data['items'] = Cart::content();
        $data['total'] = Cart::total();
        return view('cart.cart', $data);
    }
    
    public function addToCartByQty (Request $request){
        AppModelsProduct::addToCart($request->id, (int) $request->quantity);
        return Cart::count();
    }
    
    public function addToCart ($id){
    AppModelsProduct::addToCart($id);
    return Cart::count();
    }
}

My model for products:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesStorage;

class Product extends Model {
    public function category() {
        return $this->belongsTo('AppModelsCategory');
    }
    
    public static function deleteProduct($id){
        $product = self::findOrFail($id);
        Storage::disk('public')->delete($product->image);
        self::destroy($id);
        
    }
    
    public static function editProduct($request){
        $product = self::findOrFail($request->product);
        $product->name = $request->name;
        $product->slug = $request->slug;
        $product->price = $request->price;
        $product->description = $request->description;
        $product->category_id = $request->category;
        if ($request->image){
            Storage::disk('public')->delete($product->image);
            $product->image = $request->image->store('images/products', 'public');
            
        }
        $product->save();
    }
    
    public static function getProductById($id){
        return self::finOrFail($id);
    }
    
    public static function store($request){
        $product = new self();
        $product->name = $request->name;
        $product->slug = $request->slug;
        $product->price = $request->price;
        $product->description = $request->description;
        $product->category_id = $request->category;
        $product->image = $request->image->store('images/products', 'public');
        $product->save();
    }
    
    public static function getAll(){
        return self::orderBy('slug')->get();
    }
    
    public static function addToCart($id, $qty = 1){
        $product = self::findOrFail($id);
        
        Cart::add([
            'id' => $product->id, 
            'name' => $product->name, 
            'qty' => $qty, 
            'price' => $product->price, 
            'weight' => 0
            ]);
    }
    public static function getProduct($cat, $pro){
        $product = self::where('slug', $pro)->firstOrFail();
        $product_cat = $product->category->slug;        
        //retun ($product_cat === $cat) ? $product_cat: false;
        abort_if($product_cat !== $cat, 404);
        return $product;
    }
    
    
    //use HasFactory;
}

The page view:

@extends('template')
@section('content')
<div class="row">
    <div class="col-md-7">
        <h1> {{$product->name}} </h1>
        <p>{{$product->descriprion}}</p>
        <p> Only for: ₪ {{$product->price}}</p>
        <form id="add-to-cart" method="post" action="{{url('add-to-cart')}}">
            @csrf
            <div class="number">
                <span class="minus"> - </span>
                <input type="text" value="1" readonly/>
                <span class="plus"> + </span>
                <input type="hidden" value="{{$product->id}}">
                <button class="btn btn-primary" type="submit"> Add </button>
            </div>
        </form>
        <div class="col-md-5">
            <img src="{{asset('storage/' . $product->image)}}">
        </div>
    </div>
</div>
@endsection

and my route:

Route::get('add-to-cart/{product_id}', 'AppHttpControllersShopCartController@addToCart');

Advertisement

Answer

You need to pass the $product->id in the form action’s url(). That route parameter needs to be there so that it is received in the addToCart method in controller

@extends('template')
@section('content')
<div class="row">
    <div class="col-md-7">
        <h1> {{$product->name}} </h1>
        <p>{{$product->descriprion}}</p>
        <p> Only for: ₪ {{$product->price}}</p>
        <form id="add-to-cart" method="post" action="{{url('add-to-cart/' . $product->id)}}">
            @csrf
            <div class="number">
                <span class="minus"> - </span>
                <input type="text" value="1" readonly/>
                <span class="plus"> + </span>
                <input type="hidden" value="{{$product->id}}">
                <button class="btn btn-primary" type="submit"> Add </button>
            </div>
        </form>
        <div class="col-md-5">
            <img src="{{asset('storage/' . $product->image)}}">
        </div>
    </div>
</div>
@endsection

And you have declared your route as get instead of post. Following restful conventions your route should be declared as a post route

Route::post('add-to-cart/{product_id}', 'AppHttpControllersShopCartController@addToCart');
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement