Laravel define function in controller

Updated 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