How to get string value using eloquent query

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Get a Single String Value from an Eloquent Query: Mastering Collection Manipulation

As senior developers working with Laravel and Eloquent, we frequently encounter situations where our database queries return collections of results, but what we truly need is a single scalar value—a simple string, integer, or boolean. The common pitfall is trying to treat the resulting Eloquent collection directly as a string, which results in unexpected array structures, as you observed.

This guide will walk you through the most efficient and idiomatic ways to extract that specific string value from your Eloquent queries, moving beyond simple iteration to leverage Laravel's powerful Collection methods.

Understanding the Eloquent Output

When you execute a query using methods like get(), Eloquent returns a Illuminate\Database\Eloquent\Collection object. If you select a single column (like name), this collection will contain multiple model objects, each holding that value.

Your example demonstrates this perfectly:

// This returns a Collection of models
$categoryName = Category::select('name')->where('id', request('category'))->get(); 
// Result structure: [{"name":"hey"}] or similar array notation in output

To get the actual string value ("hey") out of this collection, we need to tell Laravel exactly which attribute we want to extract.

Solution 1: The Most Direct Approach – Using pluck()

The pluck() method is arguably the cleanest and most performant way to retrieve an array of values from a relationship or query result without loading the full Eloquent models into memory if you don't need the model instances themselves.

If you only want an array containing the names, use pluck('attribute_name').

Code Example for Plucking

use App\Models\Category;

// Goal: Get an array of just the 'name' strings
$categoryNames = Category::select('name')
                        ->where('id', request('category'))
                        ->pluck('name'); // <-- This extracts only the 'name' column values

// $categoryNames will now be: ['hey'] (or an array of names)

Why this is superior: pluck() directly targets the specified column, resulting in a simple PHP array containing only the string values. It avoids the overhead of instantiating full Eloquent models when you only require data retrieval, which aligns with performance best practices emphasized by resources like https://laravelcompany.com.

Solution 2: Extracting a Single Value – Using first()

If you are absolutely certain that your query should return at most one result (e.g., based on a unique ID), you can use the first() method immediately after retrieving the collection to pull out the first item. This is useful when you expect a single string rather than an array of strings.

Code Example for First()

use App\Models\Category;

// Goal: Get the name of the single category found
$categoryName = Category::select('name')
                    ->where('id', request('category'))
                    ->first(); // <-- Returns the first Model object, or null

// To get the string value from that model:
$stringValue = $categoryName ? $categoryName->name : null; 

// $stringValue will be: "hey" (assuming the record exists)

Best Practice Note: While first() is great for single results, if your WHERE clause could return multiple rows and you only want one specific value from those rows, pluck() remains more direct.

Solution 3: Using Aggregation – The Fastest Way for Counts or Sums

If your goal isn't just getting a list of names, but rather aggregating data (like finding the total count, the minimum name, or the sum of values), Eloquent provides powerful aggregation functions directly via the select() method. This is the most performant way to get scalar results directly from the database, minimizing data transfer.

For example, to get the count of categories matching a condition:

// Get the total count directly from the database
$categoryCount = Category::where('id', request('category'))->count(); 
// $categoryCount will be an integer (e.g., 1)

Conclusion

When dealing with Eloquent queries and needing a specific string value, avoid treating the resulting collection as a raw string. Instead, embrace the Collection methods provided by Laravel:

  1. For an array of specific column values: Use pluck('column_name').
  2. For retrieving a single related model: Use first() followed by accessing the attribute (e.g., $model->name).
  3. For counting or summing data: Use count(), sum(), or max() directly on the query builder.

By mastering these Collection manipulation techniques, you ensure your Laravel applications are not only functional but also highly performant and readable. Always favor these methods when building robust data retrieval systems on https://laravelcompany.com.