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

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 :: php array remove keys keep values 
Php :: php time to date 
Php :: week day php 
Php :: php validate file type 
Php :: php has constant 
Php :: how to get ip address of pc in php 
Php :: php get part of string 
Php :: apiresource laravel 
Php :: php sort hight to low 
Php :: change arabic number to english php 
Php :: remove string after comma in php 
Php :: php rand vs mt_rand 
Php :: php timestamp to seconds 
Php :: how to change php variable value in javascript 
Php :: date in php 
Php :: htmlspecialchars in php 
Php :: a facade root has not been set laravel 7 
Php :: laravel create coma separated string from query 
Php :: validation file in laravel 
Php :: check if session is started 
Php :: laravel return view with multiple variable 
Php :: transient wordpress 
Php :: php comment 
Php :: get unique array from multidimentional array by value in php 
Php :: php strftime datetime 
Php :: file exist php 
Php :: php loops 
Php :: PHP Display Posts by Category in WordPress 
Php :: drop column table in migration if exist in laravel 
Php :: how to get http parameters in php 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =