Home > database >  The email has already been taken
The email has already been taken

Time:04-26

I'm going to edit a row using form request validate laravel, but when editing if I do not change the email, The email has already been taken error. Gives

this is from request validate EditAdmin

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules;
use Illuminate\Validation\Rule;
class EditAdmin 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'],
            'email' => ['required', 'string', 'email', Rule::unique('admins')],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ];
    }
}

this my controller AdminController

<?php

namespace App\Http\Controllers;

use App\Repositories\AdminRepository;
use App\Http\Requests\CreateAdmin;
use App\Interfaces\AdminInterface;
use App\Http\Requests\EditAdmin;
use Illuminate\Validation\Rules;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use App\Models\Admin;

class AdminController extends Controller
{
    /**
     * admin Service Interface
     * @var AdminRepository
     */
    protected $admin;

    /**
     * Inject service By Service Container
     * @param AdminRepository $admin
     */
    public function __construct(AdminRepository $admin)
    {
        $this->admin = $admin;
    }

    /**
     * Show All Admins
     * Method:get
     * Return get All admin
     * @return \Illuminate\Contracts\View\View
     */
    public function getAllAdmin():\Illuminate\Contracts\View\View
    {
        $admins = $this->admin->getAllAdmin();
        return view('admin.list',['admins'=>$admins]);
    }

    /**
     * param:Admin_id
     * method delete
     * Delete admin
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function deleteAdmin(int $id):\Illuminate\Http\RedirectResponse
    {
        $this->admin->deleteAdmin($id);
        return redirect('/admins');
    }

    /**
     * get single admin
     * method get
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function getAdmin(int $id):\Illuminate\Contracts\View\View
    {
        $admin = $this->admin->getAdmin($id);
        return view('admin.edit',['admin'=>$admin]);
    }

    /**
     * Updated Admin
     * Route:Api/Nft/$nft_id
     * Method:Put
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function editAdmin(EditAdmin $request):\Illuminate\Http\RedirectResponse
    {
        $request->validated();
        //validate all input
        // $request->validate([
        //     'name' => ['required', 'string'],
        //     'email' => ['required', 'string', 'email', Rule::unique('admins')->ignore($request->id),],
        //     'password' => ['required', 'confirmed', Rules\Password::defaults()],
        // ]);
        $data = $request->all();
        $this->admin->editAdmin($data);
        return redirect('/admins');
    }

    /**
     * create new admin
     * @param CreateAdmin $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function createAdmin(CreateAdmin $request):\Illuminate\Http\RedirectResponse
    {
        $request->validated();
        $data = $request->all();
        $this->admin->createAdmin($data);
        return redirect('/admins');
    }
}

How can I fix this bug? pleas help me!!!

CodePudding user response:

this is because of using same validator for crate and update user . and user own email in update process make trouble for you . solotion: 1- crate new validator class for edit user << UserEditRequest >> 2- use this code for validate email

            'email' => ['required', 'string', 'email,'.auth()->user()->id,]

CodePudding user response:

$request->validate(['email' => 'required|email|unique:users,email,'.$id])
    

You have to add only id at your email validation code like these :

unique:users,email,'.$id

  • Related