Search
 
SCRIPT & CODE EXAMPLE
 

PHP

unique validation on update laravel

public function rules()
{
  return [
      'email' => 'required|email|unique:users,email,'.$this->user->id,
  ];
}
Comment

laravel validation unique if this field is changed

use IlluminateValidationRule;

$request->validate([
    'phone' => [
        'required',
        Rule::unique('table_name', 'column_name')->ignore($request->id),
    ],
]);
Comment

Laravel: Validation unique on update

Just a side note, most answers to this question talk about email_address while in Laravel's inbuilt auth system, the email field name is just email. Here is an example how you can validate a unique field, i.e. an email on the update:

In a Form Request, you do like this:

public function rules()
{
  return [
      'email' => 'required|email|unique:users,email,'.$this->user->id,
  ];
}
Or if you are validating your data in a controller directly:

public function update(Request $request, User $user)
{
  $request->validate([
      'email' => 'required|email|unique:users,email,'.$user->id,
  ]);
}
Update: If you are updating the signed in user and aren't injecting the User model into your route, you may encounter undefined property when accessing id on $this->user. In that case, use:

public function rules()
    {
      return [
          'email' => 'required|email|unique:users,email,'.$this->user()->id,
      ];
    }
A more elegant way since Laravel 5.7 is:

public function rules()
{
    return [
        'email' => ['required', 'email', IlluminateValidationRule::unique('users')->ignore($this->user()->id)]
    ];
}
Comment

laravel validation unique in edit

protected $rules = [
    'email_address' => 'sometimes|required|email|unique:users',
    ..
];
Comment

laravel validation unique in edit

...
$rules = User::$rules;
$rules['email_address'] = $rules['email_address'] . ',id,' . $id;
$validationCertificate  = Validator::make($input, $rules);
Comment

Laravel 9 Unique Validation on Update

 // Using Form Request

 	public function rules()
    {
        return [
            'name'  => [
                'required',
                Rule::unique('posts')->ignore($this->id)
            ],
          ];
    }
Comment

PREVIOUS NEXT
Code Example
Php :: pdo connect 
Php :: how to add property to an object in php 
Php :: how add field to table by another migration in laravel 
Php :: php foreach 
Php :: json stringify php decode 
Php :: how to replace double quotes in a string in php 
Php :: convert multi-dimensional array into a single array in laravel 
Php :: how to know the path of php in linux 
Php :: wordpress set image quality 
Php :: laravel blade upper case 
Php :: php full day name 
Php :: wordpress get permalink taxonomy id 
Php :: compile custom/plain css with mix in laravel 
Php :: laravel clear page cache 
Php :: multidimensional array item remove php 
Php :: ::update() should not be called statically 
Php :: Excerpt/ get post content 
Php :: check current pages is a child page wordpress 
Php :: get last letter in php 
Php :: dompdf laravel page break 
Php :: creer un modele laravel 
Php :: php get first element of array 
Php :: PHP money_format — Formats a number as a currency string 
Php :: how to search by sku woocommerce 
Php :: laravel log could not be opened fix 
Php :: php array map cast to int 
Php :: Fatal error: Allowed memory size of 1610612736 bytes exhausted 
Php :: eloquent with 
Php :: public_path laravel 
Php :: laravel collection prepend 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =