How to edit a record with union fields in the Standard?

Asked

Viewed 114 times

-1

I am not able to edit the registry with single emails always says that the value already exists when I try to save the record.

URL:

http://dominio/dashboard/admin/franqueados/11/edit

REQUEST:

 public function rules()
{
    $id = $this->segment(4);

    $data = [
        'nome' => 'required|max:255',
        'document_number' => 'cnpj',
        'cep' => 'required',
        'logradouro' => 'required',
        'bairro' => 'required',
        'cidade' => 'required',
        'estado' => 'required',
        'numero' => 'required',
        'banco' => 'required',
        'agencia' => 'required',
        'conta' => 'required',
        'email' => "required|email|unique:users,email,{$id},id",
        'telefone' => 'required',

    ];


    return $data;
}

CONTROLLLER:

   public function update(FranquedoRequest $request, $id)
    {
        $data =  $request->all();

        $this->franqueado->findOrfail($id)->update($data);
        $request->session()->flash('message', 'Franqueado atualizado com sucesso');
        return  redirect()->back();
    }

1 answer

1

Inside your Franchiseadorequest, you need to tell the Newsletter to ignore the email of the user being updated.

Example:

<?php
use Illuminate\Validation\Rule;
....

public function rules()
{
    $id = $this->segment(4);

    $data = [
        'nome' => 'required|max:255',
        'document_number' => 'cnpj',
        'cep' => 'required',
        'logradouro' => 'required',
        'bairro' => 'required',
        'cidade' => 'required',
        'estado' => 'required',
        'numero' => 'required',
        'banco' => 'required',
        'agencia' => 'required',
        'conta' => 'required',
        'email' => [
             'required',
             'email',
             Rule::unique('franqueado')->ignore($id, '<campo do id caso não se chame id>')
         ],
        'telefone' => 'required',
    ];


    return $data;
}

You can check more details here:

https://laravel.com/docs/5.7/validation#Rule-Unique

Note that I used 5.7 as version of the documentation and the adjustment may vary from version to version.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.