If you use findOrFail by itself without chaining another method User::findOrFail(1)
, it will return a collection, but once you begin adding other conditions you must call either ->get()
for a collection of users, ->first
or ->last()
for a single user object or if you do something with ->lists()
you will get an array. Otherwise you will get the builder class, here's an example of a {{ dd(User::where('id', 1)) }}
Builder {#1097 ▼
#query: Builder {#1096 ▶}
#model: User {#409 ▶}
#eagerLoad: array:1 [▶]
#macros: array:4 [▶]
#onDelete: Closure {#1102 ▶}
#passthru: array:12 [▶]
}
The Builder class is like a prepared statement which was never executed. When call to dd()
you should see the something similar to the following.
This will give you a collection {{ dd(\App\User::where('id', 1)->get()) }}
Collection {#1105 ▼
#items: array:1 [▼
0 => User {#1108 ▼
#cardUpFront: false
#dates: array:3 [▶]
#table: "users"
#fillable: array:2 [▶]
#hidden: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:17 [▶]
#original: array:17 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
#forceDeleting: false
}
]
}
or to get a single object with {{ dd(\App\User::where('id', 1)->first()) }}
User {#1108 ▼
#cardUpFront: false
#dates: array:3 [▶]
#table: "users"
#fillable: array:2 [▶]
#hidden: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:17 [▶]
#original: array:17 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
#forceDeleting: false
}
and finally an array with {{ dd(\App\User::where('id', 1)->lists('email','id')) }}
array:1 [▼
1 => "godfrey17@example.net"
]
Hope this helps.