Laravel. Get single column values of a table using Query Builder
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel: Getting Single Column Values from a Query using the Query Builder
As senior developers working with Laravel, we often bridge the gap between declarative database querying (SQL) and object-oriented PHP. One of the most common points of confusion for newcomers is understanding how to correctly extract specific data—especially single column values—from the results returned by the Query Builder or Eloquent.
This post addresses a specific scenario: retrieving only the values from a few columns of a table, and ensuring we can access those results easily within our controller logic. We will explore why simple property access sometimes fails and demonstrate the most efficient methods available in Laravel.
## The Scenario: Filtering Data with the Query Builder
Let's start with the situation described. We have an `items` table, and we want to filter records based on a `ref_code`, then retrieve only specific columns (`id`, `ref_code`, `name`, `price`).
The initial attempt often looks like this:
```php
$request = request(); // Assume $request holds the input data
$itemsQuery = DB::table('items')
->where('ref_code', '=', $request->ref_code)
->get(['id', 'ref_code', 'name','price']);
```
When you call `->get([...])`, Laravel returns a **Collection** of results. This Collection contains multiple records, and each record is itself an object or an associative array, depending on how you structure the request.
## Why Direct Property Access Fails
The confusion arises when trying to treat this resulting Collection as if it were a single row. When you try to access `$items->id`, PHP attempts to resolve that property on the main `Collection` object itself, which does not exist in the way you expect. The result is either an error or an empty value because the Collection itself doesn't possess the singular column data you are looking for; it possesses a list of rows.
As demonstrated in your initial investigation using `dd($items)`, you see that `$items` is indeed a `Collection`:
```php
// Example output from dd($items)
Collection {#325 ▼
#items: array:1 [▼
0 => {#322 ▶} // This is the actual item record
]
}
```
To get individual column values, we must explicitly tell Laravel *how* to transform that collection into a simpler structure.
## Solution 1: Retrieving Full Records (When You Need Multiple Rows)
If your goal is to retrieve all matching items, the `get()` method is correct. You then iterate over the results:
```php
$items = DB::table('items')
->where('ref_code', '=', $request->ref_code)
->get(); // Get all columns for matching rows
foreach ($items as $item) {
echo "ID: " . $item->id . ", Name: " . $item->name;
}
```
This is the standard approach when you need the complete data set. For more complex relational queries, mastering the structure provided by the Query Builder is fundamental to building efficient applications on platforms like Laravel.
## Solution 2: Retrieving Single Column Values Efficiently (The Best Practice)
If your requirement is strictly to find a single value—for instance, finding the `id` for a specific `ref_code`, or getting an array of just the `name`s—you