Home Laravel How to Call a Controller Function in Another Controller in Laravel?

How to Call a Controller Function in Another Controller in Laravel?

by Shahriar Sagor

Today now in this post we will know How to Call a Controller Function in Another Controller in Laravel? . so let’s see a tutorial of laravel call function from another controller. let’s start and discuss laravel call controller method from the another controller. So in this tutorial i will give you a very simple example of how to use controller in another controller laravel. Here I want to show you how we can call controller function in another controller in laravel.

Sometimes, we have to call the controller method in another controller, that’s is the reason laravel provides, the several ways to call the controller function in any another controller. Without any further ado, let’s see the below code example.

So In this example, i will create “OtherController” with the two method with public “exampleFunction()” and static “exampleFunctionStatic()” method. Abd then i will call those functions in “PostController”.

so let’s start and see below three ways.

Create OtherController

let’s create one controller for our examples.

OtherController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class OtherController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function exampleFunction()
    {
        return "From Another Controller Text";
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    static function exampleFunctionStatic()
    {
        return "From Another Controller Text";
    }
}
Example 1:

PostController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Http\Controllers\OtherController;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $result = (new OtherController)->exampleFunction();
  
        dd($result);
    }
}
Example 2:

PostController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $result = app('App\Http\Controllers\OtherController')->exampleFunction();
  
        dd($result);
    }
}
Example 3:

PostController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $result = OtherController::exampleFunctionStatic();
  
        dd($result);
    }
}

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like