Validation laravel compare 2 fields not to repeat in the database?

-1

I need an if to verify 2 values of the register and 2 of the bank, if both are equal should not occur the registration.

<div class="container-fluid">

    <form action="adiciona" method="post"  >
      <div class="form-group has-success has-feedback">

      <input type="hidden" 
      name="_token" value="{{{ csrf_token() }}}" />

      <label>Solicitação:</label>
      <select name="Cadastro_idCadastro" lass="js-data-example-ajax">
            @foreach($cadastro as $c)
            <option value="{{$c->idCadastro}}">{{$c->idCadastro}}</option>
            @endforeach
      </select><br></br>
      <label>Usúario:</label>
      <select name="users_id" lass="js-data-example-ajax">
            @foreach($user as $c)
            <option value="{{$c->id}}">{{$c->name}}</option>
            @endforeach
      </select><br></br>

      <label>Aprovador:</label>
      <select name="Aprovado_idaprovado" lass="js-data-example-ajax">
            @foreach($aprovar as $c)
            <option value="{{$c->idaprovado}}">{{$c->descaprovado}}</option>
            @endforeach
      </select><br></br>

      <button type="submit" class="btn 
        btn-primary btn-block">Adicionar Aprovador</button>
    </form>
public function adiciona()
{
    Aprovadores::create(Request::all());    
    return redirect()->action('AprovadoresController@novo');
}
  

    
asked by anonymous 30.10.2017 / 14:00

1 answer

0

Your question is a bit confusing, but I think it's a validation of this kind, where the two fields have to be unique in the table, below a minimal example of how to do this:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class  {

    public function adiciona(Request $request)
    {
        $cadastro_idcadastro = $request->Cadastro_idCadastro;
        $users_id = $request->users_id;

        $request->validate([
            'cadastro_idcadastro' => [
                'required',
                Rule::unique('aprovadores')->where(function ($query)
                use ($cadastro_idcadastro, $users_id)
                {
                      return $query->where('cadastro_idcadastro', $cadastro_idcadastro)
                                   ->where('users_id', $users_id);
                }),
            ]
        ]);
        // se passar para essas linha os dados são válidos.

        Aprovadores::create(Request::all());    
        return redirect()->action('AprovadoresController@novo');
    }

}

References:

30.10.2017 / 19:09