Search
 
SCRIPT & CODE EXAMPLE
 

PHP

unique validation on update laravel

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

how to validate use unique in laravel 8 controller

 $request->validate([
            'name' => 'required',
            'email' => 'required|unique:Student',
            'password' => 'required|min:6',
            'mobile' => 'required',
            'address' => 'required',
        ]);
//unique:table_name
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 unique validation

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */

public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    // The blog post is valid...
}
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

laravel unique validation

unique:table,column,except,idColumn

############## Example : ################

############### For Updating  
//rules
'email' => 'unique:users,email_address,' . $userId,

############### For Creating 
//rules
'email' => 'unique:users,email_address',
Comment

laravel validate form data unique

'username' => ['required', Rule::unique('users', 'username')],
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 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

 // Using Form Request

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

PREVIOUS NEXT
Code Example
Php :: laravel throw exception with status code 
Php :: destroy session php 
Php :: Add Laravel .env variable to Vue component 
Php :: convert date in php 
Php :: php time format 
Php :: laravel grouping routes 
Php :: wordpress debug true 
Php :: how to add property to the request object 
Php :: bcrypt laravel 
Php :: nova resource title function 
Php :: remove html tags from a string except p in php 
Php :: laravel request validation boolean 
Php :: php script to calculate next 50 days from current date 
Php :: How to use php to set title 
Php :: phph get server protocol 
Php :: woocommerce my account url 
Php :: strupper 
Php :: redirect wordpress 
Php :: add like and equal in same query in laravel 
Php :: laravel model quard 
Php :: The blade is not updated with minor changes to the first blade 
Php :: integer division in php 
Php :: docker invalid port 80 
Php :: php get end date of month 
Php :: how can get url from $request in laravel 
Php :: ajax update method in php pdo 
Php :: is_array php 
Php :: laravel optional route parameter in middle of url 
Php :: How to set a comment on table using Laravel Schema 
Php :: php 8 major changes - Nullsafe operator 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =