Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Laravel 5.0 Creating Request Handler

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 313
    Comment on it

    In Lavarel one of the best practice is to use form validation rules to validate the submitted data. There are number of ways to do it . One way is to create form validation rules both in the controller or we can create a separate file under the app folder inside Requests folder and define our rules within it.

    The main objective is to lighten our controller and also use the validation n number of times when ever needed. We pass it to the controller method and it automatically gets called when the form gets submitted and validate the data.

    Following are the step by step to accomplish it :-

    1st Step :- Create a new file under/app/Http/Requests/CreateCompanyDepartmentRequest.php

    2nd Step:-

    <?php
        namespace App\Http\Requests;
        use App\Http\Requests\Request;
        class CreateCompanyDepartmentRequest extends Request {
             /**
             * Determine if the user is authorized to make this request.
             *
             * @return bool
             */
            public function authorize() {
                return true;
            }
    
            /**
             * Get the validation rules that apply to the request.
             *
             * @return array
             */
            public function rules() {
                return [
                    'department_name' => 'required|min:3',
                    'department_number' => 'required'
                ];
            }
        }
    

    3rd Step:- Include that file in the controller

    use App\Http\Requests\CreateCompanyDepartmentRequest;
    

    4th Step Use it in the method in the controller

    public function store(CreateCompanyDepartmentRequest $request, CompanyDepartment $cd, Company $company) {
    
    .......
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: