Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

381
Vistas
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 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda