How to save the ID of a autocomplete field in the table with Laravel?

Asked

Viewed 115 times

0

Good morning, guys, how are you? I have a problem here, I created a view and a form for this view, in this form have some fields that autocomplete with data from some tables, the question now, is that I need to save the ID of this data in a new table but I’m not getting, Can someone please help me? Follow the codes:

Controller:

public function salvaAtividdade(Request $request) {

    $servico = $this->manutencaoServicoM
        ->select('descricao', 'id')
        ->get();

    foreach ($servico as $services) {
        $atividade = New ManutencaoAtividade();
        $atividade->id_serv = $services->id;
        $atividade->id_maq = $request->id;
        $atividade->id_user = $request->colaborador_id;
        $atividade->descricao = $request->descricao;
        $atividade->data = $request->data;
        $atividade->tempo = $request->tempo;

        dd($atividade);
    }

    if ($atividade->save()) {
        return redirect()->route('manutencao')->with('Manutenção cadastrada com sucesso!');
    } else {
        return redirect()->route('manutencao')->with('Erro ao cadastrar manutenção, tente novamente!');
    }

_form.:

<div class="container">
<div class="form-row">
    <div class="form-group col-md-4">
        <label for="tipo"><h4>Classificação do Serviço: </h4></label>
        <select name="tipo" id="tipo" class="form-control">
            <option value="1">Preventiva</option>
            <option value="2">Corretiva</option>
            <option value="3">Planejada</option>
            <option value="4">Limpeza</option>
        </select>
    </div>

    <div class="form-group col-md-4">
        <label for="servico"><h4>Serviço: </h4></label>
        <input name="servico" id="servico" class="form-control" placeholder="Digite o serviço a ser associado...">
    </div>

    <div class="form-group col-md-4">
        <label for="maquina"><h4>Maquina: </h4></label>
        @foreach($maquina as $maquinas)
            <input name="maquina" id="maquina" class="form-control" value="<?=$maquinas->descricao?>">
            <input type="hidden" name="maquina" id="maquina" value="<?=$maquinas->id?>">
        @endforeach
    </div>
</div>

<div class="form-row">
    <div class="form-group col-md-4">
        <label for="usuario"><h4>Técnico: </h4></label>
        <input name="usuario" id="usuario" class="form-control" placeholder="Digite o nome do técnico...">
    </div>

    <div class="form-group col-md-4">
        <label for="data"><h4>Data do Serviço: </h4></label>
        <input type="date" name="data" id="data" class="form-control">
    </div>

    <div class="form-group col-md-4">
        <label for="tempo"><h4>Tempo do Serviço: </h4></label>
        <input type="time" name="tempo" id="tempo" class="form-control">
    </div>
</div>

<div class="for-row">
    <div>
        <label for="descricao"><h4>Descrição da manutenção feita: </h4></label>
        <textarea name="descricao" id="descricao" class="form-control" rows="10"></textarea>
    </div>
</div>

View.:

@extends('portal.template')

@Section('Jstopo')

<script>
    $( function() {
        var availableServices = [
            <?=$nomes?>
        ];
        $( "#servico" ).autocomplete({
            source: availableServices
        });

        var availableUsers = [
            <?=$userNames?>
        ];
        $( "#usuario" ).autocomplete({
            source: availableUsers
        });
    });
</script>

@endsection

@Section('content')

<h2>Cadastro de manutenção</h2><hr>

<div class="container form-group col-md-8">

    {!! Form::open(['route'=>'manutencao-save-atividades',  'method'=>'post', 'onsubmit'=>'ShowLoading()']) !!}

    @include('portal.manutencao.servicos._atividades-form')

</div>

<div class="container form-group col-md-8">

    {!! Form::submit('Salvar', ['class' => 'btn btn-primary form-control']) !!}

    {!! Form::close() !!}
</div><hr>

@endsection

The controller I passed is just the part to save the data.

From now on I appreciate any help.

Cordially.

  • What are the fields with problem?

  • Good morning Leonardo, the fields I need and are not getting are those of machine id, service id and employee id. I don’t know how exactly I can bring these fields so I can insert them into the table.

1 answer

1

Thanks guys I managed to settle here:

I added two lines to my controller to compare and get the information I needed, my controller now looks like this:

public function salvaAtividdade(Request $request) {

    $servico = ManutencaoServicos::select('id')->where('descricao', '=', $request->servico)->first();

    $user = Colaborador::select('id')->where('nomecolaborador', '=', $request->usuario)->first();

    $atividade = New ManutencaoAtividade();
    $atividade->id_serv = $servico->id;
    $atividade->id_maq = $request->maquinas;
    $atividade->id_user = $user->id;
    $atividade->descricao = $request->descricao;
    $atividade->data = $request->data;
    $atividade->tempo = $request->tempo;

    if ($atividade->save()) {
        return redirect()->route('manutencao')->with('Manutenção cadastrada com sucesso!');
    } else {
        return redirect()->route('manutencao')->with('Erro ao cadastrar manutenção, tente novamente!');
    }
}

Worked well now, thanks for the help.

Hugs.

Browser other questions tagged

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