Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I add validation to my CRUD operations in Laravel


How can I add validation to my CRUD operations in Laravel


To add validation to your CRUD operations in Laravel, you can follow these steps:

1. Validate the Request in the Controller:
- In the controller methods that handle the CRUD operations (e.g., `store()`, `update()`), use the `validate()` method to validate the incoming request data.
- For example, in the `store()` method of your `PostController`:

php
     public function store(Request $request)
     {
         $validatedData = $request->validate([
             'title' => 'required|max:255',
             'content' => 'required',
         ]);

         $post = Post::create($validatedData);

         return redirect()->route('posts.show', $post->id)
                          ->with('success', 'Post created successfully.');
     }
     

- The `validate()` method will automatically throw a `ValidationException` if the validation fails, which you can handle in your application's exception handler.

2. Display Validation Errors in the View:
- In your Blade views, you can display the validation errors using the `$errors` variable, which is automatically available in the view.
- For example, in your `create.blade.php` or `edit.blade.php` view:

html
     @if ($errors->any())
         <div class="alert alert-danger">
             <ul>
                 @foreach ($errors->all() as $error)
                     <li>{{ $error }}</li>
                 @endforeach
             </ul>
         </div>
     @endif

     <form method="POST" action="{{ route('posts.store') }}">
         @csrf
         <div class="form-group">
             <label for="title">Title</label>
             <input type="text" class="form-control @error('title') is-invalid @enderror" id="title" name="title" value="{{ old('title') }}">
             @error('title')
                 <div class="invalid-feedback">{{ $message }}</div>
             @enderror
         </div>
         <div class="form-group">
             <label for="content">Content</label>
             <textarea class="form-control @error('content') is-invalid @enderror" id="content" name="content">{{ old('content') }}</textarea>
             @error('content')
                 <div class="invalid-feedback">{{ $message }}</div>
             @enderror
         </div>
         <button type="submit" class="btn btn-primary">Submit</button>
     </form>
     

- The `@error` directive is used to display the validation error messages for each field.

3. Customize Validation Rules:
- You can customize the validation rules based on your specific requirements. Laravel provides a wide range of built-in validation rules, such as `required`, `max`, `email`, `unique`, and more.
- You can also create custom validation rules if needed.

By following these steps, you can easily add validation to your CRUD operations in Laravel, ensuring that the data entered by users meets your application's requirements.

Citations:
[1] https://magecomp.com/blog/crud-operation-laravel-8/
[2] https://kinsta.com/blog/laravel-crud/
[3] https://techvblogs.com/blog/laravel-9-crud-application-tutorial-with-example
[4] https://tonymasek.com/blog/how-to-make-laravel-reverb-work-on-laravel-forge
[5] https://dev.to/novu/the-ultimate-guide-to-laravel-reverb-275o