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 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 on Update

 // Using Form Request

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

PREVIOUS NEXT
Code Example
Php :: php array remove key value pair 
Php :: curl in laravel 
Php :: globals in php 
Php :: create project laravel 
Php :: installing bootstrap on laravel8 
Php :: laravel multiple orderby 
Php :: static modal 
Php :: php sort multidimensional array by value 
Php :: php button to another page 
Php :: laravel update model from request 
Php :: laravel check if exists in table 
Php :: sanctum auth check? 
Php :: Server Requirements in laraavel 9 
Php :: laravel store method 
Php :: laravel middleware route 
Php :: default value date symfony entity 
Php :: fetch data from live website curl php 
Php :: laravel range query 
Php :: run seeder in migration laravel 
Php :: string remove last two characters php 
Php :: changing created_at to short date time 
Php :: Determining if input is present in Laravel 
Php :: pdo bind param 
Php :: check if date between two dates laravel 
Php :: php ping 
Php :: query string in laravel 
Php :: laravel tree 
Php :: laravel fortify 
Php :: wordpress get order 
Php :: php if 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =