Laravel 6 unique on validation

Viewed 31

I am trying to validate data during user update profile...the update works perfectly fine when there is no validation, but when i try to validate data using requests i am unable to make change in user data i get the duplicate entry error when i try to update for example a name field and leave email field as it is...

This is what i have tried... UpdateProfileRequest Request below

<?php

namespace App\Http\Requests\User;

use Illuminate\Foundation\Http\FormRequest;



class UpdateProfileRequest 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|string|max:255|unique:users,'.$this->id, 
            'organization'=>'required',
            'location'=>'required',
            'email'=>'required',
            'email' => 'required', 'string', 'email', 'max:255', 'unique:users'.$this->id,
            /*'password'=>'required'*/
            
        ];
        
   
    }
}

UseController...update method

   public function update(UpdateProfileRequest $request)
    {
           
        $user = auth()->user();

        $user->update([
            'name' => $request->name,
            'organization' => $request->organization,
            'location' => $request->location,
            'email' => $request->email,
            /*'password' => Hash::make($request->password)*/
        ]);

Below is my form in edit.blade.php in view

            <form method="POST" action="{{ route('users.update') }}">
                @csrf
                @method('PUT')

                <div class="form-group row">
                    <label for="name" class="col-md-4 col-form-label text-md-right"><i class="fa fa-user" style="color:#3490dc;"></i> {{ __('Name') }}</label>

                    <div class="col-md-6">
                        <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ $user->name }}" required autocomplete="name" autofocus>

                    </div>
                </div>


                <div class="form-group row">
                    <label for="organization" class="col-md-4 col-form-label text-md-right"><i class="fa fa-building-o" style="color:#3490dc;"></i> {{ __('Organization') }}</label>

                    <div class="col-md-6">
                        <input id="organization" type="text" class="form-control @error('organization') is-invalid @enderror" name="organization" value="{{ $user->organization }}" required autocomplete="organization" autofocus>

                   
                    </div>
                </div>


                <div class="form-group row">
                    <label for="location" class="col-md-4 col-form-label text-md-right"><i class="fa fa-map-marker" style="color:#3490dc;"></i> {{ __('Location') }}</label>

                    <div class="col-md-6">
                        <input id="location" type="text" class="form-control @error('location') is-invalid @enderror" name="location" value="{{ $user->location }}" required autocomplete="location" autofocus>

                   
                    </div>
                </div>



                <div class="form-group row">
                    <label for="email" class="col-md-4 col-form-label text-md-right"><i class="fa fa-envelope" style="color:#3490dc;"></i> {{ __('E-Mail Address') }}</label>

                    <div class="col-md-6">
                        <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ $user->email }}" required autocomplete="email">

                    
                    </div>
                </div>

                <!--<div class="form-group row">
                    <label for="password" class="col-md-4 col-form-label text-md-right"><i class="fa fa-key" style="color:#3490dc;"></i> {{ __('Password') }}</label>

                    <div class="col-md-6">
                        <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
                        <small>Enter password/<small style="color:#3490dc;">New password</small></small>
                    </div>
                </div>

                <div class="form-group row">
                    <label for="password-confirm" class="col-md-4 col-form-label text-md-right"><i class="fa fa-key" style="color:#3490dc;"></i> {{ __('Confirm Password') }}</label>

                    <div class="col-md-6">
                        <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                        <small>Retype password/<small style="color:#3490dc;">New password</small></small>
                    </div>
                </div>-->

                <div class="form-group row mb-0">
                    <div class="col-md-6 offset-md-4">
                        <button type="submit" class="btn btn-primary">
                            {{ __('Update') }}
                        </button>
                    </div>
                </div>
            </form>

User Model

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends \TCG\Voyager\Models\User
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'organization', 'location', 'email', 'password', 
       
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    


}

I want to edit profile such that name and email address are unique, but allow same data for current user despite giving the duplicate entry error for the same user...

I will appreciate any help thank you in advance

1 Answers

Replace rules function in UpdateProfileRequest class with below function:

public function rules()
{   
    return [
            'name' => ['required','string', 'max:255',
                        Rule::unique('users')->where(function ($query) use($this) {
                            return $query->where('name', $this->name);
                        })->ignore($this->id)
                    ],
            'organization'=>'required',
            'location'=>'required',
            'email' => ['required','string', 'email', 'max:255',
                        Rule::unique('users')->where(function ($query) use($this) {
                            return $query->where('email', $this->email);
                        })->ignore($this->id)
                    ]
        ];
}
Related