How to Create Laravel Unique Validation on Multiple Columns?
Teams applying “How to Create Laravel Unique Validation on Multiple Columns?” in a production project can use the platform page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Jan 24, 2023
Hi Dev,
I'll show laravel unique validation on many columns in this tutorial. You may learn about the two-field, unique Laravel validation here. This article will show you a straightforward example of laravel unique column validation. You'll discover how to alter two columns for unique Laravel validation.
I'll explain how to utilise unique validation on a number of fields in a Laravel 6, Laravel 7, Laravel 8, or Laravel 9 application in this section. I'll give you several options for including two columns of unique validation in your application. Both at create time and edit time, you can add unique validation.
All company has a distinct login, hence sometimes we need to provide unique validation with multiple fields for username etc. Then, if you want to check a unique email address or username, you must manually build a database query and utilise an if condition. However, Laravel offers "unique" rules that make it simple to apply unique validation.
Basically, you can create your own request class using bellow command:
Create Request Class:
php artisan make:request StoreUserRequest
php artisan make:request UpdateUserRequest
Controller File Code:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\User;
class UserController extends Controller
{
public function store(StoreUserRequest $request)
{
/* Do Something */
}
public function update(UpdateUserRequest $request, User $user)
{
/* Do Something */
}
}
Now you can see bellow solution one by one and you can use anyone that you require for unique validation.
Example 1: Store Unique Validation on Multiple Columns
app/Http/Requests/StoreUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreUserRequest 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()
{
return [
'name' => 'required',
'email' => 'required|email',
'username' => [
'required',
Rule::unique('users')
->where('company_id', $this->company_id)
]
];
}
}
Example 2: Update Unique Validation on Multiple Columns
app/Http/Requests/UpdateUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'username' => [
'required',
Rule::unique('users')
->ignore($this->user)
->where('company_id', $this->company_id)
]
];
}
}
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial