Updating Multiple Records using Eloquent (Laravel 5.4)

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Updating Multiple Records using Eloquent (Laravel 5.4) Introduction: In the world of software development, data management is crucial for any application to perform efficiently and effectively. Laravel provides a robust Eloquent ORM that helps developers interact with relational databases in a clean and elegant manner. This blog post aims to present an efficient method of updating multiple records using Eloquent (Laravel 5.4). In the query you provided, you're making use of the DB facade to modify multiple records directly through the database table. While this approach works, it lacks the power and flexibility that Laravel's Eloquent ORM offers. Let us explore an alternative solution with the help of Eloquent models. 1. Preparing the Models: Before updating the records, we need to define our Eloquent model for exercises. Create a new file 'Exercise.php' in your application's corresponding folder (e.g., 'app/Models'), and add the following code:
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Exercise extends Model {
    protected $table = 'exercises'; // Set the table name for the model
}
2. Fetching Records: Now, we will need to collect the required records that you want to update. You can either pass a list of IDs or perform a query on your database based on specific criteria. We'll go with the latter approach in this example. Suppose you want to update all exercises whose name contains "Pull Up". Use the following code:
$pullUps = Exercise::where('name', 'LIKE', '%Pull%Up')->get();
3. Updating Records: We have our list of pull-up exercises ready for updating. The next step is to iterate through each exercise and update its desired attributes using the Eloquent's update() method. Here's a sample code that updates all the attributes at once:
foreach ($pullUps as $exercise) {
    $exercise->update($request->all());
}
In this example, we are assigning all request parameters to the update function. It's a common practice in Laravel to pass an array with the keys that you want to update and their values as part of the second argument. If you have specific attributes to update, you would update them individually:
foreach ($pullUps as $exercise) {
    $exercise->update(['name' => 'Pull Up', 'description' => 'Explained']);
}
4. Dealing with Massive Data: If you have a large number of exercises to update, performing the updates one record at a time could be time-consuming and resource-intensive. To optimize performance in such cases, you can use Laravel's bulk updating feature. First, create a dedicated model for your bulk operation, as shown below:
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PullUpsBulk extends Model {
    protected $table = 'pull_ups_bulk'; // Define a separate table for bulk updates

    public function exercises() {
        return $this->belongsToMany(Exercise::class, 'exercises_pull_up_relationships'); // Relate with Exercise model via pivot table
    }
}
Now, you can update all your records in bulk by performing a join between the exercises and pull_ups_bulk tables. Use the following code:
$pullUpsBulk = PullUpsBulk::with('exercises')->create(['status' => 'Pending']);

foreach ($pullUps as $key => $exercise) {
    // Get pull up bulk record for each exercise, if not found, create one
    $bulkRecord = $pullUpsBulk->update()->whereHas('exercises', function (Builder $query) use ($exercise) {
        $query->whereId($exercise->id);
    })
    ->first();

    if (!$bulkRecord) {
        $pullUpsBulk = PullUpsBulk::with('exercises')->create(['status' => 'Pending']);
    }

    // Update your model attributes here, e.g., $bulkRecord->update(['name' => 'Pull Up', 'description' => 'Explained'])
}
Conclusion: This blog post has provided a thorough analysis of updating multiple records using Eloquent ORM in Laravel 5.4. It offers practical code examples and best practices to help you optimize your application's data management. Remember, it is always recommended to choose the most suitable approach based on your specific requirements. In some cases, using direct database queries may be more efficient than Eloquent models, whereas in others, using bulk operations can significantly improve performance. As a senior developer and technical blogger, practice and experience will sharpen your skills in making these decisions effectively.