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.
thanks, this is very helpful