
How to call controller function from view in Laravel
You can call controller function from view in Laravel by defining the controller method as a static method and call the method from view file by specifying the full path of controller class.
Answers 1
-
Get user details by calling controller's method from view in Laravel
//resources\views\home.blade.php @php echo App\Http\Controllers\HomeController::getUserByID(1); @endphp //app\Http\Controllers\HomeController.php //Create controller (HomeController) if not already created using php artisan make:controller HomeController and use below function in it. public static function getUserByID($id){ $user = User::findOrFail($id); return $user; }
0Output:
{"id":1,"name":"john","email":"[email protected]","email_verified_at":"2021-08-02T00:38:40.000000Z",
"created_at":"2021-08-02T00:38:40.000000Z","updated_at":"2021-08-02T00:38:40.000000Z"}This code snippet helps you to call the controller's method from view file. You have to create a web route, a function with static method in controller and you can call this method from view file by specifying the full path of controller class by passing argument to controller's method.
Random Code Snippet Queries: Laravel
- Get products with number of orders in Laravel
- Send post data from controller to view
- Laravel form request validation
- How to get laravel errors folder in views directory in laravel
- If no route matched route::fallback in laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Laravel order by date not working
- Shorter syntax for whereHas with call back function in laravel
- Display first n record from collection in laravel view
- Route prefix with auth middleware in laravel
- Route [password.request] not defined
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Input file with max size validation in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Retain selected value of select box in Laravel
- Laravel recursive function in controller
- How to pass two variables in HREF in laravel
- How to display pivot table column value in laravel
- How to upload multiple images after preview in laravel using cropper js
- How to increment column value of table in Laravel
- Get the post details if it has at least one comment in comments table
- How to check data inserted or deleted in pivot after toggle method
- Composer\Exception\NoSslException
- Class "App\Http\Controllers\Auth\Verified" not found
- How to make Copy or Duplicate table row in laravel