How to Call a controller function in another controller Laravel
How to Call a controller function in another controller Laravel
In this session, we will try our hand at solving the "How to Call a controller function in another controller Laravel".
I have a user controller function `getUser()` and i want to call this controller function in home controller function. How can i call a controller function in another controller in Laravel.-
Call a controller function from another controller in Laravel
You can easily call a controller function using
(new OtherController)->method()
in another controller by importing controller class withuse App\Http\Controllers\UserController;
after namespace in controller.Syntax for calling controller method.
$result = (new OtherController)->method();
Example code:
app\Http\Controllers\UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function getUser(){ return User::first(); } }
app\Http\Controllers\HomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\UserController; class HomeController extends Controller { public function getUserFromUserControllerMethod(){ $result = (new UserController)->getUser(); print_r($result); } }
routes\web.php
Route::get('/get-user', [HomeController::class, 'getUserFromUserControllerMethod']);
Output:
App\Models\User Object ( [fillable:protected] => Array ( [0] => name [1] => email [2] => password [3] => username ) [hidden:protected] => Array ( [0] => password [1] => remember_token ) [casts:protected] => Array ( [email_verified_at] => datetime ) [connection:protected] => mysql [table:protected] => users [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [preventsLazyLoading] => [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [escapeWhenCastingToString:protected] => [attributes:protected] => Array ( [id] => 1 [name] => Mr. Jay Price Jr. [email] => ziemann.eldora@example.net [email_verified_at] => 2022-03-03 11:01:39 [password] => $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi [remember_token] => q0i9UhitUF [username] => [created_at] => 2022-03-03 11:01:39 [updated_at] => 2022-03-03 11:01:39 ) [original:protected] => Array ( [id] => 1 [name] => Mr. Jay Price Jr. [email] => ziemann.eldora@example.net [email_verified_at] => 2022-03-03 11:01:39 [password] => $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi [remember_token] => q0i9UhitUF [username] => [created_at] => 2022-03-03 11:01:39 [updated_at] => 2022-03-03 11:01:39 ) [changes:protected] => Array ( ) [classCastCache:protected] => Array ( ) [attributeCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [rememberTokenName:protected] => remember_token [accessToken:protected] => )
-
Call controller from another controller
$users = app('App\Http\Controllers\UserController')->getUser();
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to randomly get the user id from users table in laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Laravel change date format
- Composer\Exception\NoSslException
- How to send ID to another page in Laravel
- How to get all route list
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- Get count of filter data, while return a small set of records
- How to get all posts which contains comments in laravel
- How to get CSRF token in laravel controller
- Remove array keys and values if it does not exist in other array in Laravel
- How to validate form input data in laravel
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- How to authenticate admin users in Laravel ?
- How to get random string in Laravel
- Input file with max size validation in laravel
- Insert Comma Separated Values in laravel
- How to get tomorrow and yesterday date in laravel
- Update existing pivot table data in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to generate .env file for laravel?
- Laravel 11 project setup on localhost using breeze with blade step by step
- How to customize pagination view in laravel
- Method chaining in Laravel
- How to display order by null last in laravel