Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Bucle a través de una matriz de solicitud

Tengo una matriz worker_id[] para una lista de selección múltiple en mi formulario de esta manera:

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

Y en mi controlador, quiero llamar a una función addRepairWorker en un bucle para que inserte valores de la matriz worker_id .

 if ($request->isMethod('post')) { $this->validate($request, [ 'worker_id' => 'required' ]); foreach ( arguments ){ $this->repairsService->addRepairWorker($request, $vehicle); } }

¿Qué argumentos necesito usar en foreach ? ¿O hay alguna otra manera? Estoy usando Laravel 5.

EDITAR:

Aquí está la función addRepairWorker :

 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 answers
Answer question

0

Cambiaría la función en sí para insertar cualquier worker_id asignado en DB

Entonces en el controlador

 if ($request->isMethod('post')) { // perform validation : // if all validations successful call function $this->repairsService->addRepairWorker($request, $vehicle); }

Ahora la función manejará múltiples valores de worker_id:

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

Si espera que esa función también se pueda llamar como valor único, simplemente agregue una verificación si la entrada es una matriz.

over 4 years ago · Santiago Trujillo Report

0

No sé qué hace la función RepairService, pero creo que estás intentando esto:

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

Esto pasará el worker_id y el vehículo para cada opción múltiple seleccionada.

Editar:

Su validación también es incorrecta. No validas matrices de solicitud como esa.

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

Edición 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!