Laravel pluck but combining first name + last name for select

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel Pluck but Combining First Name + Last Name for Select As a senior developer working with Laravel and modern frontend stacks like Vue, we often deal with the challenge of transforming raw database data into a consumable format. One common requirement is to retrieve specific fields—perhaps an ID and a user-friendly full name—and present them in a flattened structure, especially when feeding data into libraries like Select2 which expect simple key-value pairs. You correctly identified that Laravel's `pluck()` method is perfect for retrieving arrays of a single column (e.g., `Customer::pluck('id')`). However, directly combining fields across multiple columns into a single new field within the result set requires a slightly different approach than standard `pluck()`. This guide will show you the most robust and efficient ways to achieve your goal: combining `first_name` and `last_name` into a single `text` field when preparing data for your frontend. --- ## The Challenge: Combining Data for Frontend Display Let’s start by defining the desired output structure, which is common when feeding data into JavaScript frameworks: ```json [ { "id": 0, "text": "enhancement" }, { "id": 1, "text": "bug" } ] ``` We need to go beyond simple column selection and perform a calculated operation on the Eloquent model before we extract the final data. ## Solution 1: Using Eloquent Select with Database Concatenation (The Efficient Way) The most efficient way to handle this is by instructing the database itself to concatenate the names. This keeps the query efficient, as the heavy lifting is done by the database engine rather than in PHP memory. We use the `select()` method combined with the `DB::raw()` helper for string manipulation. Assuming you have a `Customer` model: ```php use Illuminate\Support\Facades\DB; use App\Models\Customer; class CustomerController extends Controller { public function getSelectableCustomers() { $customers = Customer::select( 'id', DB::raw("CONCAT(first_name, ' ', last_name) AS text") // Concatenate names in SQL ) ->get(); // Now format the result for Select2/frontend consumption $formattedData = $customers->map(function ($customer) { return [ 'id' => $customer->id, 'text' => $customer->text, // The concatenated name is already in the 'text' field ]; }); return $formattedData; } } ``` ### Explanation of the Approach: 1. **`select('id', DB::raw("CONCAT(first_name, ' ', last_name) AS text"))`**: This tells Eloquent to retrieve the `id`, and crucially, it creates a new virtual column named `text` by concatenating the first name, a space, and the last name directly in the SQL query. 2. **`->get()`**: Executes the query and returns a collection of `Customer` models where each model now has an accessible `text` attribute containing the full name (e.g., "John Doe"). 3. **`->map(...)`**: Since we want the exact array format required by Select2, we use the `map` method on the resulting collection to transform the Eloquent objects into the desired simple associative arrays. This method ensures that you are leveraging the power of your database for data manipulation, which aligns with best practices in building scalable applications supported by frameworks like Laravel. For deeper insights into optimizing these operations, understanding how Eloquent builds queries is key, especially when dealing with complex relationships or raw expressions, as detailed on [laravelcompany.com](https://laravelcompany.com). ## Solution 2: Post-Retrieval Collection Manipulation (Alternative) If concatenating names in the database seems overly complex for your specific needs, you can retrieve the data separately and combine it within PHP using collection methods. This is simpler to read but involves more data transfer from the database. ```php public function getSelectableCustomersSimple() { $customers = Customer::all(); // Fetch all necessary data $formattedData = $customers->map(function ($customer) { return [ 'id' => $customer->id, // Combine the names in PHP memory 'text' => $customer->first_name . ' ' . $customer->last_name, ]; }); return $formattedData; } ``` While Solution 2 is straightforward, **Solution 1 (Database Concatenation)** is generally preferred for performance, especially when dealing with large datasets. It reduces network latency and delegates the string manipulation to the highly optimized SQL layer. ## Conclusion To achieve your goal of using `pluck`-like functionality while combining fields in Laravel, move beyond simple column selection. Instead, leverage the power of raw SQL expressions within Eloquent's `select()` method, utilizing functions like `CONCAT()`. This hybrid approach—combining database efficiency with clean PHP collection mapping—provides a powerful and scalable solution for preparing complex data structures for your frontend applications.