Hi Dev,
Today,I will learn 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 RouteIn this setp,I will create route for a call controller method from another method in laravel.
<?php use App\Http\Controllers\UserController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/home', [UserController::class,'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:UserControllerand
php artisan make:AdminControllerStep 3:User Controller
In this setp, i will writer code of call controller method from another method.
<?php 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.
<?php 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....
No comments:
Post a Comment