Cast Laravel query results to class
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Data Hydration: Casting Laravel Query Results to Custom Classes
When working with databases in any framework, the initial result of a query is typically returned as a collection of generic objects. In the context of Laravel's Query Builder, when you execute methods like DB::table('users')->get(), the resulting data is hydrated into an array of standard PHP objects, specifically stdClass.
The core question we often face is: How do we take this generic set of properties and map them cleanly onto our specific domain models (e.g., a User class or a custom Foo class)? Simply casting the result using a type hint like (Foo) $result will obviously fail, as PHP does not support direct object casting in this manner for dynamic data structures.
This post will dive into the most robust and idiomatic ways to handle this data hydration in Laravel, moving beyond simple typecasting to implement professional-grade data mapping.
The Eloquent Advantage: Let the ORM Handle the Heavy Lifting
Before diving into raw query results, it is crucial to understand that when using Laravel’s Object-Relational Mapper (ORM), Eloquent models handle this casting automatically and elegantly. When you retrieve data using an Eloquent model, the framework automatically instantiates your corresponding class for every row returned from the database.
For instance, if you fetch users:
use App\Models\User;
$users = User::all(); // This returns an array of User objects, not stdClass.
This is the preferred approach because Eloquent manages relationships, accessors, mutators, and casting seamlessly. It ensures that your data layer remains clean and highly maintainable, which aligns perfectly with the philosophy behind modern frameworks like Laravel.
Handling Raw Query Results: Mapping stdClass to Custom Classes
The challenge arises when you are using the raw Query Builder methods—like DB::table('foo')->get()—which return simple stdClass objects. To convert these into instances of a custom class, we need an explicit mapping strategy.
There are several effective developer-focused ways to achieve this hydration:
Method 1: Manual Mapping with Collection Iteration
The most straightforward way is to iterate over the results and manually instantiate your target class for each row, mapping the properties accordingly. This provides full control over the transformation process.
class Foo
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
// Assume $stdClassResult is the result from DB::table('foo')->get()
$stdClassResult = (object)['id' => 1, 'name' => 'Alice'];
$foos = $stdClassResult->map(function ($row) {
return new Foo($row->id, $row->name);
});
// $foos is now an array of Foo objects.
Method 2: Using Collection::map for Cleaner Transformation
For collections returned by the Query Builder, leveraging Laravel’s Collection methods makes the transformation cleaner and more functional. You can iterate over the result set and use the map operation to return new instances based on the raw data. This aligns well with how developers manage data sets in modern PHP applications.
use Illuminate\Support\Facades\DB;
$results = DB::table('foo')->get();
$foos = $results->map(function ($row) {
// Assuming 'id' and 'name' are the columns returned by the query
return new Foo($row->id, $row->name);
});
Conclusion: Prioritizing Eloquent for Data Integrity
While it is technically possible to cast stdClass objects into custom classes using manual iteration or collection mapping techniques, these methods introduce boilerplate code that can become cumbersome as applications grow.
As senior developers, our primary goal should be to leverage the framework’s built-in strengths. For any application dealing with relational data in Laravel, we strongly advocate for using Eloquent Models. They abstract away the complexities of database interaction, automatically perform hydration, manage relationships, and ensure data integrity across your entire system. Relying on Eloquent keeps your codebase cleaner, more readable, and fully aligned with best practices promoted by Laravel.