Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

379
Visualizações
Insert into multiple input fields using select2 and ajax in laravel

I am trying to use a single select request to retrieve data from database based on the search term "name" and populate my input fields using select2 so that i don't need to fill in the forms again after the selection but seems not getting anywhere as i have search across the web but not able to find anything that solve the problem

here is my select request

<script type="text/javascript">
$('.serviceId').select2({
    placeholder: 'Select Service',
    allowClear: true,
    ajax: {
        url: '/autocomplete/fetch',
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: $.map(data, function (item) {
                    return {
                        text: item.name,
                        id: item.id,
                        description:item.description,
                        rate:item.rate
                        
                    }
                })
                
            };
        },
        
        cache: true
    }
});

and here is the form;

   <select class="form-select form-select-solid fw-bold serviceId" name="serv_name"></select>


                        
                    </td>
                    <td>
                        {{ Form::text('description[]', null, ['class' => 'form-control form-control-solid']) }}
                    </td>
                    <td class="table__qty">
                        {{ Form::number('quantity[]', null, ['class' => 'form-control qty form-control-solid','required', 'type' => 'number', "min" => 1]) }}
                    </td>
                    <td>
                        {{ Form::text('rate[]', null, ['class' => 'form-control price-input rate form-control-solid','required']) }}
                    </td>

and the controller file

/**
*show the application fetch
*
*@return \Illuminate\Htttp\Response
*/

public function fetch(Request $request) {
    $data = [];
    if ($request->has('q')) {
        $search = $request->q;
        $data = DB::table('services')
                ->orderby('name','asc')
                ->select('id','name', 'description', 'rate')
                ->where('name', 'like', '%' .$search . '%')
                ->limit(5)->get();
        
              }

    return response()->json($data);
}

my route: web.php

Route::get('/autocomplete/fetch', 'InvoiceController@fetch')->name('autocomplete.fetch'); 

i only need the qty field to be hard code by the user.

sorry am not so good with codes and any valuable help would be much appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

why you gave class to your select there can be multiple dropdown?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda