Laravel query builder returns object or array?
- Model::find(numeric); returns a object
- Model::whereId(numeric)->first(); returns a object
- Model::whereId(numeric)->get(); - returns a collection
- Model::whereId(numeric); - returns a builder
get()
returns a collection of objects every time. That collection may have 0 or more objects in it, depending on the results of the query.
first()
calls get()
under the hood, but instead of returning the collection of results, it returns the first entry in the collection (if there is one).
Which method you use depends on what you need. Do you need the collection of all the results (use get()
), or do you just want the first result in the collection (use first()
)?