How to Call Post Methods from Laravel Controllers

Bryan Fajardo
Nov 11, 2019 · 3 min read
Photo from Pixabay

Sometimes you may come across a need to call a post method from another function in Laravel. These methods may or may not be in the same controllers/classes.

Say you have a very complex method that accepts Illuminate\Http\Request $request as the argument. The method is normally called from a form or AJAX (Asynchronous Javascript and XML) function. So how would you go about calling that method from another method — maybe even in another class?

Below we have a very complex method we inherited and don’t want to have to rewrite. The other method is where we want to use the complex method’s return.

So what can be done?

A Non-Preferred Solution

We can create a “Calling” function. Copy the actionable parts of the method into another function that can be called. In essence, you are creating a function that retrieves the data from the POST request and then calls the actual function that processes the data bypassing the data as a parameter. So now when veryComplexMethod is called, it will extract the data from the request and pass it into veryComplexMethodProcessing which is the real method.

  • Don’t Repeat Yourself — you are adhering to the DRY principle. You don’t have to rewrite any code.
  • Refactoring — You have to move things around and change things.
  • Inconsistency — Not all of your methods may need this, so you will end up with some functions using a calling function and some not.
  • In the scenario where you are consistent and all your POST methods do have a calling function, you just increased the number of functions you have to deal with. Your best bet would probably be to make a generic calling function.

A Preferred Solution

Let’s create a class that will create a request, and then execute any method we want while passing that request as an argument. In other words, we will try to mimic what a form or AJAX call would do.

For the sake of this example we will call the class POST_Caller.

Let’s start by declaring some variables.

Now, let’s setup the constructor.

Finally, let’s create the caller function.

This solution addresses some issues that the previous solution does not. First, it becomes a generic caller function that works for any method that expects a request in any class. Second, it is actually sending a Request as the argument, which may or may not be important to the functionality of the method you are calling. You also don’t have to refactor the method you are trying to call. In the previous solution we had to move the part that extracted the data from the Request to the caller function, and then pass the extracted data to the method that uses that data.

Let’s dissect the POST_Caller call() method.


Here you are creating a Laravel service container instance and binding to it the class of your choice. You get some good things by using the service container this way, here are a few of them.

  1. Don’t need to use the new keyword.

2. This returns an instance of the class which you can use right away in the same line.

3. You do not need to import the class namespace into the scope of POST_Caller through the use keyword.

Plus other potential benefits that come with the service container.


Call a specific method inside of the class we passed in. Using the {} syntax we can dynamically call a method. So if my class was TestingClass::class and the method was runTest. This would be the equivalent of app(TestingClass::class)->{'runTest'}().


We create an Illuminate request from a Symfony Instance. We will pass in our requestClass which is most likely to be anIlluminate\Http\Request but can also be a custom FormRequest.

return $response

Then we return whatever response that method returned to us. It is up to the method that is using this class to define how to deal with the return data.

Finally, we want to put this to use. This is our POST_Caller class in action.

These are just a couple of ways we can call a method that expects a Request from another method in Laravel. There are probably many more ways to do this. If you have any questions, want to point something out, or share an alternative — comment below!

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Bryan Fajardo

Written by

Full stack web developer. Your mind is your biggest asset, grow it!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Bryan Fajardo

Written by

Full stack web developer. Your mind is your biggest asset, grow it!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store