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

1.5K
Views
Laravel Update Multiple Records with Different Values for Each Record

I want to update multiple records with different values for each record. What should I do with the laravel eloquent? Here's my code example. Thanks in advance.

    $employee_presences = Employee_presence::
    where('employee_id', $report_history->employee_id)
    ->whereBetween('presence_date',[$report_history->report->start_period,$report_history->report->end_period])
    ->get();

    foreach ($presences_data as $presence_data) 
    {
        foreach ($employee_presences as $employee_presence)
        {
            $updated_presences = Employee_presence::
            where('id', $employee_presence->id)->update([
                'presence_value' => $presence_data['presence_value']
            ]);
        }
    }

These are the values inside $presences_data. This is the new data for updating the records.

enter image description here

These are the records $employee_presences:

enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

There's no way of updating multiple records with different data each on the same query. You can bulk update your records by running one query per group of changes:

Employee_presence::whereIn('employee_id', <list of ids>)->update(
    [
        'presence_value' => 1,
        'presence_date' => "2021-07-12"
    ]
);

Employee_presence::whereIn('employee_id', <different ids>)->update(
    [
        'presence_value' => 0.5,
        'presence_date' => "2021-07-13"
    ]
);

If there are no groups (meaning every single record will have a different value), you would need to update them separately in different queries. Not very efficient.

over 4 years ago · Santiago Trujillo Report

0

It is possible by using the mysql CASE statement. AFAIK there isn't a build in function in eloquent or the query builder for case statements, so you would have to do that part of the query with raw statements.

I don't really understand which value you want for which record in your example (may be my bad), but i will try my best to help you with a function you can use in general:

suppose you have a selection of employee id's from employees you want to update, and for every employee id you also know the presence value you want to give them. You could make an array $data where those id's are the key and the presence value the value. then you can throw that array in the following function:

public static function updatePresencePerEmployee($data){
    //make the complex case statement first
    $case = "(case";
    foreach ($data as $employee_id => $presence_value ){
        $case.=" when employee_id = ".$employee_id." then ".$presence_value;
    }
    $case.=" end)";

    DB::table('employee_presence')
        //shift the keys of the array which are employee id's to values so laravel can use it in the whereIn clause
        ->whereIn('employee_id', array_keys($data))
        //dont forget to throw the case string in to the DB:raw function
        ->update(['presence_value' =>DB::raw($case)]);
}

I use this function in one of my applications itself but it needs improvement. For instance right now you need to be sure those values in the case statement arent an sql injection.

If you don't understand what happens, reading this thread would help. it tells you how you can use the case statement.

MySQL - UPDATE multiple rows with different values in one query

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!