What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()
When working with Eloquent models in Laravel, selecting the right query method isn't just about syntax; it's about defining your contract with the database and handling failures gracefully. The distinction between these methods revolves around two main axes: how many records you expect to retrieve (single vs. multiple) and how strictly you handle cases where no data is found.
Retrieving Single Models: find() vs. first()
The primary difference here lies in how the database query is constructed when looking for a specific record by its primary key.
find(int $id) attempts to retrieve a single model instance. If the record exists, you get the model. If it does not exist, Eloquent throws a ModelNotFoundException. This method uses an INNER JOIN logic implicitly because it assumes the ID provided actually belongs to the table.
In contrast, first() is designed to fetch the first row matching a query condition. When used without arguments (or with specific filter conditions), it simply returns the first record found or null if none exist. It will not throw an exception.
// Throws ModelNotFoundException if user does not exist
$user = User::find(1);
// Returns null if no user exists, never throws
$user = User::first();
If you need a single record but want to fail fast with an exception, use findOrFail(). This is equivalent to calling find() and immediately throwing an exception if the model is not found. It is the standard pattern for controllers where you expect a resource to exist:
$user = User::findOrFail(1); // Throws ModelNotFoundException on failure
Similarly, firstOrFail() retrieves the first record matching your conditions but throws an exception if no results are returned. This is useful when you have a specific query condition (like filtering by slug) and want to ensure exactly one result exists without returning null.
Retrieving Multiple Models: get() vs. list()
When your logic requires a collection of records, get() is the default method. It returns an Eloquent Collection containing all models matching the current query scope. You can chain methods like limit(), where(), or orderBy before calling it.
$users = User::all()->get();
// Returns a collection of all users
You might wonder why you often see .toArray() appended to these calls. The issue is that Eloquent Collection objects are not standard PHP arrays. While they support array-like syntax (e.g., foreach, count), some external libraries or API responses strictly require a native array type hint. Converting the collection with toArray() serializes the models into plain arrays, stripping off the model magic and relations unless explicitly preserved.
Regarding list(), this method was introduced in Laravel 9. It returns an array where the keys are the primary key values of the models, making it ideal for simple lookups or passing data to functions expecting indexed lists. However, note that if you have duplicate primary keys (e.g., soft deletes or specific configurations), list() behavior may vary slightly compared to a standard collection.
Converting Data Structures: toArray()
The toArray() method converts an Eloquent Collection into a plain PHP array. This is essential when interfacing with frameworks like Symfony, sending JSON responses via response()->json(), or passing data to functions with strict type declarations expecting array. Without this conversion, you risk runtime errors or unexpected behavior in parts of your application that don't expect model instances.
Trade-offs and Best Practices
Choosing the right method depends on your error-handling strategy. If a missing resource is a critical failure (e.g., fetching a user by ID for a profile page), findOrFail() provides immediate feedback via an HTTP 404 response. If the absence of data is acceptable or handled downstream, first() or find() allows you to check existence before proceeding.
Always remember that get() returns a collection, while list() returns an associative array based on primary keys. Mixing these up can lead to subtle bugs when iterating over results or accessing specific fields by index versus key.