Single record with 2 fields verification - Laravel 5.1

1

I have a table with columns id , nome , descricao and tipo . In my Request I have:

'nome' => 'required|unique:edificacoes'

So far so good, I can not register anything with the same name, but how would I not be able to register with the same name only only of the same type?

Ex: I have the record 01 | NomeTeste | Tipo1 , if I try to register 02 | NomeTeste | Tipo2 I can not. How can I make it check the name and type to lock only if the 2 are the same?

Creating the table:

Schema::create('edificacoes', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->longText('nome');
            $table->longText('descricao')->nullable();
            $table->char('tipo', 4);
            $table->timestamps();
});
    
asked by anonymous 08.01.2016 / 18:00

1 answer

1

From Laravel 5.0+, you can use compound key as follows:

'nome'  => 'required|unique:{TABELA}{COLUNA}{EXCEÇÃO}{ID}

Following this documentation specification , I believe your case would look something like this:

'nome'  > 'required|unique:edificacoes,nome,NULL,id,tipo,$tipo'

In this rule, we are saying (in that order):

  • field name required;
  • field name for
    • table edificacoes
    • field name
    • any record (null)
    • primary key column is called id
    • Verify that the tipo
    • hits with $tipo

I suggest trying it this way:

'nome'  > 'required|unique:edificacoes,nome,NULL,id,tipo,tipo'

In the hope that Laravel understands that the second word tipo refers to the tipo column. But the documentation does not specify this, it simply says the value passed there (so in my previous example I used $tipo ) will be ignored.

If this form does not work, it means that Laravel does not have the ability to identify through the column name, so you have to specify the value of $tipo manually.

    
08.01.2016 / 18:27