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

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

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 Report

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 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!