Hello Devs,
In this tutorial, we will learn Laravel Call Controller Method From Another Method Example
Follow this step-by-step guide below.
Step 1: Create Route
Route::get('/home', 'UserController@index')->name('home');
Step 2: Create Controller
php artisan make:UserController
and
php artisan make:AdminController
Step 3:User Controller
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
create method in admin controller
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"
May this example help you.