Laravel Validation Unique

0

I have a registration form, and there is information for popular two tables:

  • User (User);
  • Shipper (Sender).

I created the Request of Laravel to isolate the validation rule from the controller form. I have cloquei unique in two places, the first in the field email of the user, and the second in the document of the person.

Problem

Validation only works for the first case, as long as it does not apply to the second. The result is to take an exception in the face if the CPF of the face is equal and try to register.

link

Controller

class RegisterSenderPersonController extends Controller
{
    public function store(SenderPersonUserStoreRequest $request)
    {
        $sender = Sender::create([
            'document' => $request['document'],
            'zipcode' => $request['zipcode'],
            'telephone' => $request['telephone'],
            'birthday' => $request['birthday'],
            'gender' => $request['gender'],
            'city_id' => $city->id,
        ]);

        if($sender->id) {
            $user = User::create([
                'name' => $request['name'],
                'email' => $request['email'],
                'sender_id' => $sender->id,
                'password' => $request['password'],
            ]);

            return 'gravou';
        }

        return 'deu ruim';
    }
}

SenderRequest

return [
            'birthday' => 'nullable|date',
            'document' => ['unique:sender', 'required', 'min:11','max:14', new Cpf],
            'email' => 'unique:user|required|min:4|max:35',
            'gender' => 'nullable|boolean',
            'name' => 'required|min:4|max:30',
            'password' => 'required|min:8|max:20',
            'telephone' => 'required|min:11|max:14',
            'zipcode' => 'required|min:8|max:9',
        ]; 

SenderMigration

Schema::create('sender', function (Blueprint $table) {
            $table->increments('id');
            $table->string('company_name', 60)->nullable()->unique();
            $table->string('exhibition_name', 25)->nullable();
            $table->string('document', 14)->unique();
            $table->date('birthday')->nullable();
            $table->boolean('gender')->nullable();
            $table->string('zipcode', 8);
            $table->string('telephone', 11);
            $table->boolean('active')->default(true);
            $table->timestamps();
        });
    
asked by anonymous 07.08.2018 / 03:36

1 answer

0

According to the laravel documentation, it is necessary to pass one more column parameter in the rules

  

unique: table, column, except, idColumn

     

The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.

Try this:

return [
        'birthday' => 'nullable|date',
        'document' => ['unique:sender,document', 'required', 'min:11','max:14', new Cpf],
        'email' => 'unique:user,email|required|min:4|max:35',
        'gender' => 'nullable|boolean',
        'name' => 'required|min:4|max:30',
        'password' => 'required|min:8|max:20',
        'telephone' => 'required|min:11|max:14',
        'zipcode' => 'required|min:8|max:9',
    ]; 
    
28.08.2018 / 04:28