Laravel 8 Form Request Validation for Unique Validation on insert & update data

By Guest | Dec 18, 2021 | Laravel
Share :

https://www.fundaofwebit.com/post/laravel-8-form-request-validation-for-unique-validation-on-insert-update-data

Laravel 8 Form Request Validation for Unique Rule Validation insert & update data 


In this post, we will be learning how to make a validation using Form Request in Laravel 8 for insert and update data into database in laravel 8. Where we also understand as Laravel Unique Validation on Store & Update.

So guys, for example we will take Student Model, which has name, email, phone, etc. Now, let's make the email as unique under form request in laravel 8.

Step 1: Create a Request with the following command:

$ php artisan make:request StudentFormRequest


Step 2: go to your controller and use this StudentFormRequest in store and update function as follows:

<?php

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;
use App\Http\Requests\StudentFormRequest;

class StudentController extends Controller
{
    public function store(StudentFormRequest $request)
    {
        //your code
$data = $request->validated();
        Student::create($data);
        return redirect('/students')->with('message','Student Added Successfully');
    }

    public function update(StudentFormRequest $request, Student $student)
    {
        //your code
        $data = $request->validated();
        $student->fill($data);
        $student->save();

        return redirect('/students')->with('message','Student Updated Successfully');
    }
}
?>


Step 3: Unique validation with Rule using Form Request Validation for same in laravel 8

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class StudentFormRequest extends FormRequest
{
    /**
     * 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()
    {
        $rules = [
            'fullname' => [
                'required',
                'string',
                'max:255',
            ],
            'phone' => [
                'required',
                'digits:10',
            ],
            'course' => [
                'required',
                'string',
                'max:255',
            ],
        ];

        if($this->getMethod() == "POST")
        {
            $rules += [
                'email' => [
                    'required',
                    'string',
                    'email',
                    'unique:students,email',
                    'max:255',
                ],
            ];
        }

        if($this->getMethod() == "PUT")
        {
            $student = $this->route('student');
            $rules += [
                'email' => [
                    'required',
                    'string',
                    'email',
                    'max:255',
                    Rule::unique('students')->ignore($student->id),
                ],
            ];
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'fullname.required' => 'Please Enter full name',
            'email.required' => 'Please Enter Email ID',
            'email.email' => 'Please Enter Valid Email Id',
            'phone.required' => 'Please Enter Phone No',
            'course.required' => 'Please Enter Course',
        ];
    }
}


Thanks for reading.

https://www.fundaofwebit.com/post/laravel-8-form-request-validation-for-unique-validation-on-insert-update-data

Share this blog on social platforms