Validation Can you compare 2 fields not to repeat in the database?

Asked

Viewed 1,089 times

0

I need a if to check 2 values of the register and 2 of the bank, if both are equal should not occur the register.

<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');
}

inserir a descrição da imagem aqui

  • What’s the mistake? Only the code is vacant.

  • sorry, I need a se to check 2 values of the register and 2 of the bank, if both are equal should not occur the registration. my controller Approvers::create(Request::all(); Return redirect() ->action('Approvedscontroller@new');

  • following which values cannot be repeated if you can put the table in your question?

  • idCadastro and id of the user.

  • pass me the table Aprovadores (you shouldn’t do that kind of Insert either?

  • idAp, Cadas_ipcadatro, users_id, Approved

  • (shouldn’t one do that kind of Insert too? ) I don’t understand

  • was testing do Insert with create(Resquest)

  • (shouldn’t one do that kind of Insert too? ) always specify the fields that are inserted never let the responsibility remain for the table this can cause future problems! , without delay put your table so that I understand what you want to do! ta half vague.

  • don’t know how to use stack over right, you have skype?

  • then it’s time to learn friend, put in your question the layout of the table and explain your doubt better I can help you around here.

  • Okay, I need to use validation to not let you save two equal compos to not duplicate, I need to take 2 fields in the database and compare them with two form fields, https://laravel.com/docs/5.5/validation I’m TRYING TO FIND, BUT NOTHING ELSE

  • I guess you don’t need to put table.

  • it was good to put the table

  • see if you get it.

  • users_id registration, both can only have once in the database

  • version of the Standard?

  • Laravel 5.5 ta in the title rsrs

  • put on the tag will learn.

  • What tag ? Oh you squeezed me.

Show 15 more comments

1 answer

1

Your question is a little confusing, but I believe it is a validation of this type, where the two fields have to be unique in the table, below a minimum 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:

  • great, I can put inside the Approvadoresrequest or need to stay inside the method ?

  • any of the two solutions put themselves in Formrequest need not put in the method and vice versa! of course AprovadoresRequest has to be in place of Request $request to make it work.

  • gave this error ("Undefined Property: Illuminate Support Facades Request::$cadastro_idcadastro")

  • @Vagner is the same registration is in the form.

  • @Vagner also needs to read the documentation

  • had tried Undefined Property: Illuminate Support Facades Request::$Cadastro_idcadastro"

  • Unfortunately the problem is that you do not show the right place, I do not know how to help you more @Vagner is complicated when one speaks German and the other speaks Brazilian... understand!

  • I put the same form with the same error.

  • @Vagner something that is already wrong, its variables one is higher and other minuscule a general mess, can not, has to have a pattern, usually all minuscule and short names at most with underscore, understood ...

  • And I also don’t know the reality of your code @Vagner

  • I understand, I’ll organize myself better.

  • Thank you! helped a lot.

Show 7 more comments

Browser other questions tagged

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