Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitsolanki24_'s avatar

Eloquent::first() vs DB facade

Hey everyone, I hope you all are doing well.

I have a question when I use eloquent first() and then access object as array it's working but when I get first using DB facade then it is giving error Cannot use object of type stdClass as array why?

When I use Eloquent

 $user = User::where('email', $email)->first();
dd(gettype($user)); // "object"
dd($user['id'], gettype($user)); //10 "object"

When I use DB facade

 $user = DB::table('users')->where('email', $email)->first();
dd(gettype($user)); // "object"
dd($user['id'], gettype($user)); //This line generate error 

Cannot use object of type stdClass as array

could you please help me in this, why this is happening?

0 likes
9 replies
Nabisalay's avatar

The difference in behavior between Eloquent's first() method and the DB facade's first() method lies in the type of object they return.

When using Eloquent, like User::where('email', $email)->first(), it creates an instance of the User model class with mapped properties from the database. So, $user is an object of the User class, allowing access like $user['id'].

On the other hand, using the DB facade like DB::table('users')->where('email', $email)->first() returns a generic PHP object without specific structure or methods. Hence, accessing $user['id'] throws an error because it lacks the model's property structure. It might have the id property, but you need to access it through its actual name, like $user->id.

In most cases, Eloquent is preferred due to its readability, type safety, and seamless relationship handling. Use the DB facade only for specific raw SQL queries or when dealing with tables without corresponding models.

I hope this will help!

2 likes
amitsolanki24_'s avatar

@Nabisalay

When using Eloquent, like User::where('email', $email)->first(), it creates an instance of the User model class with mapped properties from the database. So, $user is an object of the User class, allowing access like $user['id'].

Object means properties can only be accessed by arrow operator -> but it's also access as an array.

If $user is an object of the User class then user properties must be access by only arrow operator.

JussiMannisto's avatar

@amitsolanki24_

If $user is an object of the User class then user properties must be access by only arrow operator.

Not true. The Model class implements ArrayAccess.

1 like
amitsolanki24_'s avatar

@JussiMannisto okay that's why it's accessible by both object and array.

But what is best / recommended way to get Eloquent modal properties using object or array?

Nabisalay's avatar

@jlrdw You're absolutely right. While the example $user = DB::table('users')->where('email', $email)->first(); uses the query builder for building the specific query, the DB::table syntax does indeed indicate the use of the DB facade for initiating database interaction.

Laravel offers two main options: Eloquent ORM and DB facade. In this case, the facade is used, and it will return a generic PHP object. This object's properties are accessed using arrow notation (->) like $user->id instead of array syntax ($user['id']).

1 like
amitsolanki24_'s avatar

@Nabisalay okay thank you, It's a query builder but when I was writing this post I forgot what was the name then I typed DB facade.

1 like

Please or to participate in this conversation.