ads

Laravel Validation is most important for any type of project you wanna create, this validation will help you to define the particular syntax for every name like suppose you take email than we can define 'email' => 'required|email', Using validation we got proper error display that error to the user so the user can know about what is wrong into this application

By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules

laravel validation example

Rule:1 Laravel Eloquent Resources - Automatic Validation

This example is related suppose you are making some hotel management system and you
need to apply proper validation on your laravel project then this is the best way to apply laravel advance validation, you are storing data like below function of controller

App/Http/Controller/BookController.php


 public function store(BookRequest $request)
    {
        try{
            $data = Arr::except($request->all(), ['_token','submit']);
            $store = $this->model->create($data);
            if( $store ){
                Alert::success('Guest Details', 'Successfully Added');
                return redirect()->route($this->route_path . '.index');
            }
            Alert::error('Oops something went wrong', 'Please try again');
            return redirect()->route($this->route_path . '.index');
        }catch(Exception $e){
            return redirect()->route($this->route_path . '.index');
            Alert::error('Oops something went wrong', 'Please try again');
        }
    }



 Above Store method i declare one request name is 
 BookRequest that BookRequest must
 add in controller using like this -- 
 use app\Http\Resource\Resources\BookRequest
  



public function rules()
    {
        $room_id = $this->route('room');
    
        switch($this->method()) {

            case 'DELETE':
            case 'POST': 
                return [
                    'guest_id' => 'required|numeric',
                    'room_id' => 'required|numeric',
                    'adult' => 'required|numeric',
                    'child' => 'required|numeric',
                ];
            case 'PUT':
            case 'PATCH':
                return [
                    'guest_id' => 'required|numeric',
                    'room_id' => 'required|numeric|unique:room_id',
                    'adult' => 'required|numeric',
                    'child' => 'required|numeric',
                ];
            default:
                break;
            }
    }
    
    public function messages()
    {
        return [
            'guest_id' => 'Guest field is required',
            'room_id' => 'Room field is required',
            'adult' => 'Adult field is required ',
            'child' => 'Child field is required',
        ];
    }



Rule:2 Simple validation by the controller

  public function store(Request $request)
    {
            $request->validate([
                'first_name' => 'required',
                'last_name' => 'required',
                'employee_number' =>'required|numeric',
                'email_address' => 'required|email',
                'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
                'password'=>'required|max:8'
               ]);
    }

Rule:3 Simple validation display in View



@if ($errors->any())
@foreach ($errors->all() as $error) {{ $error }} @endforeach
@endif


If you like this Laravel validation rules list example you can share this blog and comment also if you have any doubts and query related that.

Thank you.

Post a Comment

Previous Post Next Post