Laravel query builder returns object or array?

Laravel query builder returns object or array?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Why get() and first() Return Different Types in Laravel

When working with the Laravel Query Builder, it is easy to assume that querying a single row should always yield an object, regardless of the method used. However, if you pass a query result directly to a view, you may encounter unexpected behavior where one controller returns a model instance while another returns an array. This discrepancy stems from how the underlying Eloquent and Query Builder methods handle collection retrieval versus single item retrieval.

In your ProfileController, the line $workout = DB::table('workouts')->where('user_id', '=', $user->id)->get(); executes a query that retrieves all matching records from the database. The get() method is designed to return an instance of Illuminate\Database\Eloquent\Collection. Even if only one row exists in the database, this method wraps that single result inside a collection object. When you pass this collection directly to your view, Laravel's Blade engine iterates over it as a list. If you attempt to access properties like $workout->name on a collection containing one item, it fails because collections do not expose model properties directly; they require array-style indexing or iteration.

Conversely, in your WorkoutController, the line $workout = DB::table('workouts')->where('name', '=', $name)->first(); uses the first() method. This method is explicitly designed to retrieve a single record and return it as an instance of the Eloquent model (e.g., App\Workout). It does not wrap the result in a collection. Consequently, when passed to the view, $workout behaves like a standard object, allowing direct property access such as $workout->name.

The confusion arises because both methods query the same table but operate under different assumptions about data volume. The get() method assumes you might need multiple rows and provides an iterable collection. The first() method assumes you expect exactly one row and optimizes for single-object access. If you use get() on a query that matches only one record, you must always access the first item via $workout[0] or cast it to an array using $workout->toArray().

To resolve this inconsistency in your codebase, you should align your data fetching strategy with the specific needs of each view. If a view expects a single model instance for direct property access, use first(). If a view is designed to iterate over multiple records or handle an empty set gracefully without throwing errors, get() is more appropriate.

You can also consider switching from the raw Query Builder to Eloquent models if you are already using them elsewhere in your application. Defining a Workout model allows you to use syntax like Workout::where('user_id', $userId)->get(), which returns an Eloquent collection of model instances rather than plain arrays. This approach often simplifies relationship mapping and adds features like automatic mass assignment protection.

For further reading on handling data structures in Laravel, check out resources from https://laravelcompany.com, which often covers best practices for data fetching and view rendering.

Our network