Laravel define function in controller

Created at 22-Aug-2022 , By samar

Laravel define function in controller

In this tutorial, we will try to find the solution to "Laravel define function in controller" through programming.

You can define a function in controller class of Laravel. You have to use public, private or protected function keyword with function name like `public function index(){` to define a function in Laravel.
  • public function show($id)
    {
        return view('user.profile', [
            'user' => User::findOrFail($id)
        ]);
    }
    

    Example code app\Http\Controllers\UserController.php

    <?php
     
    namespace App\Http\Controllers;
     
    use App\Http\Controllers\Controller;
    use App\Models\User;
     
    class UserController extends Controller
    {
        /**
         * Show the profile for a given user.
         *
         * @param  int  $id
         * @return \Illuminate\View\View
         */
        public function show($id)
        {
            return view('user.profile', [
                'user' => User::findOrFail($id)
            ]);
        }
    }
    

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

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.