Laravel Eloquent get all in single key value pair

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Eloquent: Getting Related Data as a Single Key-Value Object

As developers working with relational databases in Laravel, one of the most common tasks is retrieving related data. When you have a standard one-to-many relationship, Eloquent excels at fetching this data efficiently. However, sometimes the desired output isn't a simple array of related models; instead, you need to aggregate that related information into a single, easy-to-access object or map.

This post will dive into a common scenario involving two tables—posts and post_metas—and demonstrate how to transform a standard Eloquent one-to-many relationship into a consolidated key-value structure.

The Scenario: Posts and Post Metas

Let's establish the context with our database schema. We have a posts table and a post_metas table, linked by a foreign key (post_id).

Posts Table:

id title content
1 Dibbert LLC ...

Post_Metas Table:

id post_id key value
1 1 code YSRSLBZ7
2 1 agent_id 32

When we use standard Eloquent eager loading to fetch a Post and its related post_metas, the result is naturally an array of related models:

[
  {
    "id": 1,
    "title": "Dibbert LLC",
    "content": null,
    "post_metas": [
      {
        "key": "code",
        "value": "YSRSLBZ7"
      },
      {
        "key": "agent_id",
        "value": "32"
      }
    ]
  }
]

While this structure is perfectly valid for relationships, if your downstream application requires these meta-data points directly as properties of the post object—like post_metas: { code: 'YSRSLBZ7', agent_id: '32' }—we need a transformation step.

The Solution: Aggregating Relationships into Objects

The goal is to pivot the data. We want to transform the array of related metadata records into an associative array (an object) keyed by the key column from the post_metas table. This requires manipulating the Eloquent Collection after it has been loaded.

We will start with fetching a post and its meta data, then use PHP collection methods to restructure the result. Although complex aggregations can sometimes be handled in pure SQL using JSON functions, the Eloquent approach offers great readability and leverages the power of the ORM effectively. This kind of data manipulation is fundamental when building robust applications, much like how you structure your models within Laravel (see resources on laravelcompany.com for best practices).

Here is the practical implementation using Eloquent:

use App\Models\Post;

// Assuming we are fetching Post with eager loaded post_metas
$post = Post::with('postMetas')->find(1);

if ($post) {
    // 1. Start with the standard relationship array
    $postMetasArray = $post->postMetas;

    // 2. Transform the array into an associative object (map)
    $postMetasObject = [];
    foreach ($postMetasArray as $meta) {
        // Use the 'key' from post_metas as the new key in the object
        $postMetasObject[$meta->key] = $meta->value;
    }

    // 3. Assign the new object back to the post model
    $post->post_metas = $postMetasObject;

    // Now, when you access it:
    // echo $post->post_metas['code']; // Output: YSRSLBZ7
}

Alternative: Using Collection Mapping for Conciseness

For a more functional and concise approach, we can leverage Laravel's Collection methods like pluck() and keyBy() to achieve the same result in fewer lines. This pattern is highly favored in modern Laravel development for data transformation:

use App\Models\Post;

$post = Post::with('postMetas')->find(1);

if ($post) {
    // Use the loaded collection of post_metas to create a map directly
    $post->post_metas = $post->postMetas->keyBy('key');
}

Notice how keyBy('key') transforms the array of meta records into an object where the keys are derived from the key column (e.g., ['code' => 'YSRSLBZ7', 'agent_id' => '32']). This is significantly cleaner and more idiomatic for Eloquent users.

Conclusion

By understanding how to manipulate Eloquent collections, we move beyond simply retrieving data into intelligently structuring it for application consumption. Whether you are dealing with one-to-many relationships or complex nested data, mastering collection methods allows you to transform raw database results into the precise object structures your business logic demands. Keep exploring the power of Eloquent; it truly simplifies the complexity of relational data!