Laravel - pluck with specified keys

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel Eloquent: Mastering Custom Transformations with Pluck and Map As developers working with Laravel, we constantly bridge the gap between our robust backend logic and the dynamic requirements of the frontend. Often, this involves fetching data from Eloquent models and reshaping it into a format that JavaScript or other APIs can easily consume. This post addresses a very common scenario: how to use methods like `pluck` to extract data while simultaneously renaming fields to match external expectations. We will explore why direct transformations can be tricky and demonstrate the most idiomatic Laravel solution for achieving complex array restructuring. ## The Limitation of `pluck()` for Complex Transformations You started with an attempt using `pluck`: ```php Sport::pluck(["id" => "value", "name" => "text"]); ``` While `pluck()` is incredibly useful for extracting a single column's values (e.g., `Sport::pluck('name')`), it is designed strictly for selecting one attribute and returning an array of those values. It does not natively support arbitrary key renaming or complex object restructuring in the way you require—mapping one set of attributes to another set of keys within each resulting item. When your goal is to take a collection of Eloquent models (like `Sport` objects) and transform each model into a simple associative array with custom keys (`text` for `name`, `value` for `id`), you need a method that allows for per-item manipulation, which points us toward the powerful `map()` method. ## The Solution: Using Eloquent's `map()` Method The most effective and readable way to achieve your desired output format is by fetching the collection first, and then applying the `map()` collection method to iterate over each model and construct the exact structure required by your frontend. Here is a step-by-step approach: ### Step 1: Fetch the Base Data First, retrieve the models you need from the database. ### Step 2: Apply the Transformation with `map()` We use `map()` to iterate over the collection of `Sport` models and transform each model into an associative array matching the required structure (`text` and `value`). ```php $sportList = Sport::all(); // Or find() based on your query $transformedList = $sportList->map(function ($sport) { return [ 'text' => $sport->name, 'value' => $sport->id, ]; }); // $transformedList will now be: // [ // ['text' => 'Football', 'value' => 1], // ['text' => 'Basketball', 'value' => 2], // ['text' => 'Volleyball', 'value' => 3], // ] ``` ### Step 3: Integrating with Pluck for Efficiency (Alternative View) While `map()` is the clearest way to rename fields, if you find yourself doing this transformation frequently on a large dataset, you can combine it with other methods for potentially better performance or conciseness. For example, if you only need the IDs and names, you could perform a `select` first, and then map those results: ```php $transformedList = Sport::select('id', 'name') ->map(function ($sport) { return [ 'text' => $sport->name, 'value' => $sport->id, ]; });