Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eddy1992's avatar

How to pass data from one controller method to the same controller method

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

0 likes
4 replies
Pendo's avatar
Pendo
Best Answer
Level 10

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);
2 likes
TorbenDaudistel's avatar

Try to also pass the $request variable like so:

$test = $this->cartSubtotal($request);
1 like
nfauchelle's avatar

@eddaddy

3 things I suggest.

1 - Change if($request->hasCookie('gh_token')) to use Cookie:: instead, then you don't need to pass Request $request at all.

2 - You are duplicating the whole "if cookie set cookie" logic in the controller methods, do it one place...

3 - Consider moving that cartSubtotal method out to the cart, and then you could do $subTotal = Cart::instance($cookie)->subTotal(); This means if you have any models or jobs (like sending a thanks email) you can easily call that, and not have to try and reference a controller method.

Just my 3 * 2c.

2 likes

Please or to participate in this conversation.