The error says it all, doens't it? You're trying to run $this->cartSubtotal() while this method expects a paramater of the type Request. I guess all you need to do is:
$test = $this->cartSubtotal($request);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi I wrote a method that would return some data which is calculated so what I want to do is that in other controller methods I should be able to call that method and get the values it returned. So I am trying to call the method like $this->methodName(). But it gives me an error :
Argument 1 passed to App\Http\Controllers\CartController::cartSubtotal() must be an instance of Illuminate\Http\Request, none given, called in /var/www/html/srsgrocery/app/Http/Controllers/CartController.php on line 121 and defined
So I want to know what is wrong ?
my cart controller method that I created which would return data when called upon.
public function cartSubtotal(Request $request)
{
if($request->hasCookie('gh_token'))
{
$cookie = Cookie::get('gh_token');
}
else
{
$cookie = $this->setCookie();
}
$cartData = Cart::instance($cookie)->content();
$subtotal = [];
$count = 0;
foreach ($cartData as $key => $value)
{
$subtotal[$count++] = $value->price;
}
$subTotal = array_sum($subtotal);
return $subTotal;
}
Now I tried to call cartSubtotal in other methods like $this->cartSubtotal();
public function viewCart(Request $request)
{
if($request->hasCookie('gh_token'))
{
$cookie = Cookie::get('gh_token');
}
else
{
$cookie = $this->setCookie();
}
$test = $this->cartSubtotal(); //this is where I called but it gives an error
print_r($test);
die;
$cartData = Cart::instance($cookie)->content();
if($cartData)
{
return view('frontend.cart', compact('cartData'));
}
else
{
return "no items in the cart";
}
}
Please help and tell me what is wrong
The error says it all, doens't it? You're trying to run $this->cartSubtotal() while this method expects a paramater of the type Request. I guess all you need to do is:
$test = $this->cartSubtotal($request);
Please or to participate in this conversation.