Laravel 10|9|8 call controller method from another controller

To access/call controller method/function from another controller in Laravel 10|9|8 apps; Through this tutorial, you will learn how to call controller method/function from another controller function/method in laravel 10, 9, 8 apps.

How to Access/Call Controller Method from Another Controller in Laravel 10|9|8

If you want to access/call controller method/function in another controller function/method. so you need to use the following example on how to call controller method/function from another controller in laravel 10, 9, 8:

Let’s say you have a TestController. And index method/function inside a TestController. Which is given:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function exampleFunction()
    {
        return "Test Controller Text";
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    static function exampleFunctionStatic()
    {
        return "Test Controller Text";
    }
}

Again, Let’s say you have a HelloController. And index method/function inside a HelloController. Which is given:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class HelloController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {


        dd('hello');
    }
}

Now you want to call TestController’s method inside HelloController’s index method/function.So any call controller method can be called from any other controller in Laravel app.

You can see in the above example how TestController’s method is called from inside HelloController’s index method/function. Given below:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\TestController;

class HelloController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $result = (new TestController)->exampleFunction();

        dd($result);
    }
}

Conclusion

Through this tutorial, you have learned how to call controller method/function from another controller function/method in laravel 10, 9, 8 apps.

Recommended Laravel Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Laravel 10|9|8 call controller method from another controller

  1. thanks, this is very helpful

Leave a Reply

Your email address will not be published. Required fields are marked *