Laravel : How to add a key value into an object?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: How to Dynamically Add Key-Value Pairs to Eloquent Models

As a senior developer working with the Laravel ecosystem, you frequently deal with collections of Eloquent models. One of the most common tasks is performing bulk operations—modifying multiple database records based on data retrieved from the application layer. Today, we are diving into exactly how to effectively add dynamic key-value pairs, like adding tags, to every record within a collection.

This isn't just about manipulating PHP arrays; it’s about understanding how Eloquent models interact with the database when you are iterating over them. Understanding this process is fundamental to efficient data management in Laravel.

The Challenge: Modifying Eloquent Models in a Collection

You are working with an $articles collection, and you want to add a new attribute, such as tags, with an array of values (["foo", "bar"]), to every single article model before saving them back to the database. Your initial attempt using $articles->map() is a great starting point, but it only manipulates the data in memory; it does not automatically persist those changes to the database.

The key challenge here is bridging the gap between the PHP Collection and the underlying persistence layer provided by Eloquent. We need a mechanism that iterates over each model instance, modifies its attributes, and then executes an update query.

Solution 1: Iterating and Updating (The Standard Approach)

The most straightforward, explicit way to achieve this is by looping through the collection and calling the save() method on each model instance. This ensures that every modification is persisted correctly to your database.

Here is how you can modify your ArticlesController.php:

public function index()
{
    $articles = user()->articles;
    // $articles is a Collection of Article models

    $tagsToAdd = ["foo", "bar"];

    // Loop the collection and add the tags to each article
    foreach ($articles as $article) {
        // Add the new attribute (the key-value pair)
        $article->tags = $tagsToAdd;
        
        // Save the changes back to the database for this specific record
        $article->save(); 
    }

    dd($articles);
    return view('articles', compact('articles'));
}

Why This Works:

When you iterate over a Laravel Collection of Eloquent models, each item is an instance of your model. By accessing $article->tags = $tagsToAdd;, you are modifying the object's properties in memory. Calling $article->save(); then triggers Eloquent to generate and execute an UPDATE query against the database for that specific article. This pattern is essential when performing record-by-record modifications.

Solution 2: Using Collection Methods (For Mass Updates)

While the foreach loop is explicit, sometimes you look for a more functional approach using collection methods. Unfortunately, there isn't a single built-in method like map() that automatically calls save() on every resulting model instance because saving involves database interaction, which should be treated as an explicit action.

However, if your goal was only to update the in-memory data without hitting the database (e.g., for temporary processing), you could use map():

$multiplied = $articles->map(function ($article) use ($tagsToAdd) {
    // This only modifies the object in memory, NOT the database
    $article->tags = $tagsToAdd;
    return $article; // Must return the item for map to collect it
});

dd($multiplied);

For persistence, stick to Solution 1. If you need high-performance bulk updates across many records, explore Eloquent's update() method or raw SQL queries, which are generally faster than iterating and saving one by one. For complex relationships, exploring Laravel's relationship management—as detailed on the official https://laravelcompany.com/ documentation—will provide powerful tools for optimizing these operations.

Conclusion

Adding dynamic key-value pairs to Eloquent models within a collection requires careful consideration of persistence. For modifying individual records, the foreach loop combined with $model->save() is the clearest and most reliable method. It ensures that your application state perfectly reflects the database state, which is critical for maintaining data integrity in Laravel applications. Always prioritize clarity and correctness when dealing with Eloquent models!