Skip to content

All topics  /  Laravel

Laravel Call Controller Method From Another Method Example

Laravel ·

Hi Guys

In this blog,I will example you how to call controller method from another method in laravel. I will show example of laravel call controller method from another method.you can easliy call controller method from another method in laravel.

I will describe step by step laravel call controller method from another method example.

Step 1: Create Route


In this setp,I will create route for a call controller method from another method in laravel.

Route::get('/home', 'UserController@index')->name('home');

Step 2: Create Controller

In this setp, I will create two controller using terminal/cmd. I will create UserController and AdminController.

php artisan make:UserController

and

php artisan make:AdminController

Step 3:User Controller

In this setp, i will writer code of call controller method from another method.

 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
    	$id = 1;
        $result = (new AdminController)->index($id);
        return view('home',compact('result'));
    }
}

Step 4:Admin Controller

In this setp, i will create method in admin controller for code of call controller method from another method.

 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Admin;
class AdminController extends Controller
{
    public function index($id)
    {
        dd('This is AdminController Index Method Id='.$id);
    }
}

OutPut

"This is AdminController Index Method And Id=1"

It will help you....