laravel how to access column with number name of a table?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel: How to Access Columns with Numeric or Custom Names in Eloquent
As developers working with relational databases through an ORM like Eloquent in Laravel, we often encounter scenarios where accessing data feels intuitive but throws unexpected errors. One common point of confusion arises when dealing with columns that have numeric names (like 22) or non-standard naming conventions. How do you correctly retrieve a specific column value?
This post will dive into the pitfalls of direct array indexing versus proper Eloquent access and demonstrate the robust methods for accessing any column, regardless of its name, ensuring your data retrieval is reliable and scalable.
The Pitfall of Direct Indexing: Why $obj[22] Fails
When you interact with a database via Eloquent, you are generally working with objects that represent rows. Attempting to access properties using simple numeric indices (e.g., $row[22]) or string keys that don't match the actual column name often leads to errors like Undefined offset or Undefined index.
This happens because PHP arrays and Eloquent results are structured based on the database schema, not a sequential, zero-indexed list of columns. If your table has columns named id, name, and 22, accessing $row[22] assumes the array is indexed sequentially starting from 0, which is not how Eloquent structures the data access for specific fields.
Let's look at the common failure scenario:
// Assuming $obj is an Eloquent model instance fetched from the database
$obj = Order::find(1);
// Attempting direct numeric or string access often fails:
$obj[22]; // Fails: Undefined offset
$obj['22']; // Fails: Undefined index
This demonstrates that relying on positional indexing is brittle and error-prone when working with object-oriented data structures provided by Laravel. For robust data access, we need methods that explicitly reference the column name.
The Solution: Dynamic Property Access in Eloquent
The most reliable way to access any field—whether it has a standard name or a numeric/custom identifier—is by using dynamic property access syntax within the object. This allows you to build the key dynamically based on a variable holding the desired column name.
If your table has a column named, for instance, column_number (which holds the value 22), you must use that actual field name when accessing it. If you are dynamically constructing the column name from a variable, dynamic property access is the key.
Consider how you can access a field using a variable:
use App\Models\Order;
// 1. Find the record
$order = Order::find(1);
if ($order) {
// Define the column name dynamically as a string
$columnName = '22'; // The actual name of your column
// Access the data using dynamic property access: $object->{$variable}
$value = $order->{$columnName};
echo "The value in column {$columnName} is: " . $value;
} else {
echo "Order not found.";
}
This method works because PHP allows you to use curly braces {} to access object properties whose names are stored in variables. This technique is highly flexible and avoids the strict limitations of array indexing, making your code cleaner and more resilient.
Best Practices: Database Design Matters More Than Code Tricks
While dynamic