Search
 
SCRIPT & CODE EXAMPLE
 

PHP

unique validation on update laravel

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

set unique value validation for laravel form request

use IlluminateValidationRule;

public function rules()
{
    return [
	    'title' => [
		    'required',
		    Rule::unique('posts', 'title')->ignore($this->post)
	    ]
    ]; 
}
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

unique validation laravel

<?php
  
namespace AppHttpRequests;
  
use IlluminateFoundationHttpFormRequest;
  
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',
                'username' => 'required|min:8',
                'email'=>'required|email|unique:users,email,NULL,id,deleted_at,NULL'
            ];
    }
}
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 :: woocommerce remove payment method when totla is 0 
Php :: jwt auth laravel auth without password field 
Php :: encrypt & decrypt laravel 
Php :: excerpt more wordpress 
Php :: laravel cache remember 
Php :: laravel validation image or file 
Php :: laravel 8 blade get days and hours ago 
Php :: php html to text 
Php :: drop column migration laravel 
Php :: how to alias table name in laravel model 
Php :: Laravel Retrieve All Session Data 
Php :: php get option value 
Php :: laravel eager loading where clause 
Php :: php geolocation package 
Php :: how to clear session in laravel 
Php :: symfony set timezone 
Php :: fetch method and class in codeigniter 
Php :: get current authenticated user laravel 
Php :: php add to multidimensional array 
Php :: laravel npm run deve error mix 
Php :: filter child table data from parent laravel eloquent 
Php :: unset php 
Php :: hide add new link on cpt page 
Php :: php error log 
Php :: Diferencia entre dias PHP 
Php :: eloquent get trashed record 
Php :: php numbers 
Php :: why session is not working in laravel 
Php :: wp image size name 
Php :: Get wordpress posts by category name..! 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =