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

144
Vistas
Loop through a request array

I have an array worker_id[] for multi select list in my form like this:

<select multiple class="form-control" name="worker_id[]" size="8">
     @foreach($workers as $worker)
          <option value= ... </option>
     @endforeach
</select>

And in my controller, I want to call a function addRepairWorker in a loop so it inserts values from the worker_id array.

if ($request->isMethod('post')) {
    $this->validate($request, [
            'worker_id' => 'required'
    ]);

    foreach ( arguments ){
            $this->repairsService->addRepairWorker($request, $vehicle);
    }
}

What arguments do I need to use in foreach? Or is there some other way? I am using Laravel 5.

EDIT:

Here is addRepairWorker function:

public function addRepairWorker(Request $request , Vehicle $vehicle){

        $workers_needed = null;
        DB::transaction(function () use ($request, $vehicle, $idcko) {

            $workers_needed = new Repair_worker();
            $workers_needed->repair_id = $idcko; //$idcko is DB query, not important here
            $workers_needed->worker_id = $request->worker_id;

            DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                    [$workers_needed->repair_id, $workers_needed->worker_id]);
        });
    }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I would change the function itself to insert any assigned worker_id into DB

So in the controller

if ($request->isMethod('post')) {
    // perform validation
    :

    // if all validations successful call function
    $this->repairsService->addRepairWorker($request, $vehicle);
}

Now the function will handle multiple worker_id values:

public function addRepairWorker(Request $request , Vehicle $vehicle){

    $worker_id_array = $request->input('worker_id');
    foreach ($worker_id_array as $worker_id) {

       DB::transaction(function () use ($request, $vehicle, $idcko) {

         $workers_needed = new Repair_worker();
         $workers_needed->repair_id = $idcko; //$idcko is DB query, not important here
         $workers_needed->worker_id = $worker_id;

         DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                [$workers_needed->repair_id, $workers_needed->worker_id]);
    });

   }
}

If you expect that function may be called also for as single value, just add a check if input is an array.

over 4 years ago · Santiago Trujillo Denunciar

0

I don't know what the function repairService does but I think you are trying to this:

for ($i = 0; $i < count($request->worker_id); $i++)
{
  $this->repairsService->addRepairWorker($request->worker_id[$i], $vehicle);
}

This will pass the worker_id and the vehicle for each multiple selected option.

Edit:

Your validation is also incorrect. You don't validate request arrays like that.

public function rules()
    {

       $rules = array();
        if(null !== $this->request->get('worker_id')){
            foreach ($this->request->get('worker_id') as $key => $val) {
                $rules['worker_id.' . $key] = 'required|integer';
            }
        }


            return $rules;
        }

Edit 2:

public function addRepairWorker($worker_id , Vehicle $vehicle){

        $workers_needed = null;


            $workers_needed = new Repair_worker();
            $workers_needed->repair_id = $idcko; //$idcko is DB query, not important here
            $workers_needed->worker_id = $worker_id;

            $workers_needed->save();
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