Mass update of product attribute text

Stefan Bogdanescu
Founder & Senior Architect · 5 September 2026

Mass Updating Product Attribute Text: A Practical Guide

Updating product attributes en masse can be a daunting task, especially when dealing with complex data structures like HTML strings containing unique content and common elements (like URLs). I’ve faced this challenge myself while working on e-commerce projects.

Understanding the Problem

In your scenario, you need to update a specific URL within each product’s delivery_text attribute without altering the rest of the text. This requires parsing the HTML string for each product and making targeted replacements.

The Laravel Approach

Laravel provides powerful tools for querying and updating database records in bulk. Here’s a step-by-step guide using Laravel 8.x:

Step 1: Query All Products with Delivery Text

First, you need to fetch all products along with their delivery_text attributes. You can use the Eloquent query builder or the fluent query syntax.

use App\Models\Product;

$products = Product::all();

Step 2: Prepare the Replacement Data

Identify the old and new URLs you want to replace.

$oldUrl = 'oldsite.com/old-url';
$newUrl = 'newsite.com/new-url';

Step 3: Update the Delivery Text in Bulk

Now, iterate through each product and use PHP’s str_replace function to make the replacement. Since delivery_text is likely stored as a string type, you don’t need to parse it as an actual HTML document.

foreach ($products as $product) {
    // Use str_replace for simple string replacement
    $updatedText = str_replace($oldUrl, $newUrl, $product->delivery_text);

    // Update the product record
    $product->update(['delivery_text' => $updatedText]);
}

This code replaces all occurrences of $oldUrl with $newUrl in each product’s delivery_text. The updates are applied in a single query for each product due to Laravel’s mass assignment feature.

Best Practices

  • Avoid Long-Living Loops: If you have many products, consider using Laravel’s chunking feature to process records in smaller batches. This can prevent timeouts and improve performance.
Product::chunk(500, function ($products) use ($oldUrl, $newUrl) {
    foreach ($products as $product) {
        // Update logic here
    }
});
  • Use Transactions: If you’re updating a large number of records, consider wrapping your updates in a database transaction. This ensures data consistency if something goes wrong during the update process.
DB::transaction(function () use ($products, $oldUrl, $newUrl) {
    // Update logic here
});

Conclusion

Updating product attributes en masse requires careful planning and execution. By leveraging Laravel’s query builder and PHP string manipulation functions, you can efficiently update thousands of records with minimal manual intervention.

Remember to test your updates in a staging environment before applying them to your production database. Also, consider using version control systems like Git to track changes to your codebase during the update process.

For more information on bulk updating in Laravel, check out this guide from Laravel Company. They provide detailed examples and best practices for working with large datasets in Laravel applications.

Our network