Laravel Unique Validation Example

Laravel Unique Validation Example

In this Laravel Unique Validation Example, you will learn how to apply different authentication and rules by installing, update data on laravel.

First, check whether or not this data exists in database tables in laravel before inserting or updating data in database tables. So at that point, you need to add a different confirmation to the columns of the data table.

Repeated laravel verification entry means that, when you enter data into a data table, it checks whether it is present in the data table. If data is available, it returns the message “column value already exists”. Otherwise it adds new entries to the data table.

Read Also :  Laravel 8 Yajra Datatable Example Tutorial

You can also exclude certain columns in your database with separate laravel authentication without.

Laravel Unique Validation Example

Here, you will learn the below step by step about laravel unique validation:

  • Simple Unique Validation on Controller
  • Unique Validation with Column Name
  • Unique Validation with Rule
  • Laravel Unique Validation on Update

Simple Unique Validation on Controller

When you insert data into database table. But first of all, you require to check data is exist or not. So you can use simple laravel unique validation.

Read Also :  Google Line Chart Example in Laravel

The belowlaravel unique validation rule is simple. And you can use it as below:

public function store(Request $request) {       
  request()->validate([
         'name' => 'required',
         'username' => 'required|min:8',
         'email' => 'required|email|unique:users',
         'contact' => 'required|unique:users'     
   ]);
 }

laravel unique validation with Column Name

The below unique validation rules with column name on controller methods, you can use as below:

public function store(Request $request) {
        request()->validate([
         'name' => 'required',
         'username' => 'required|min:8',
         'email' => 'required|email|unique:users,email',
         'contact' => 'required|unique:users'
        ]);
 }

laravel unique validation with Rule

public function store(Request $request) {
        request()->validate([
         'name' => 'required',
         'username' => 'required|min:8',
         'email' => ['required', Rule::unique('users')],
         'contact' => 'required|unique:users'
        ]);
 }

Laravel Unique Validation on Update

public function store(Request $request) {
        request()->validate([        
         'name' => 'required',
         'username' => 'required|min:8',
         'email' => 'required|email|unique:users,email,'.$this->user->id,
         'contact' => 'required|unique:users'
        ]);
 }

I hope it can help you…