Method orderBy does not exist in Laravel Eloquent?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
One of the most powerful aspects of working with Laravel's Eloquent ORM is its ability to chain method calls. However, inappropriate usage can lead to unexpected errors like BadMethodCallException, as we have seen above. In this article, we will delve into the proper way of chaining methods for complex queries and discuss how Laravel's Eloquent models are designed.
Chaining Method Calls in Laravel Eloquent
Laravel Eloquent provides a fluent interface to work with database records. A method call is chained to the previous one, allowing us to perform multiple operations on the same query without losing context.
In the given code snippet:
$products = Product::all()
if ($search_value) {
$products = $products->where('name', 'LIKE', "%$search_value%")
}
$products = $products->orderBy('created_at', 'desc')
$products = $products->skip(10)
$products = $products->take(10)
$products = $products->with('tags')
$products = $products->get()
The code performs the following steps:
- Retrieve all products in the database.
- Apply a filter based on the search value (if provided).
- Sort the results by 'created_at' field in descending order.
- Exclude 10 rows from the result to skip a few records.
- Limit the result to the next 10 products, effectively paginating the data.
- Eager-load the related 'tags' model for each product.
Notice that all these operations are chained and executed simultaneously. However, there is one issue with this code: the last method call is redundant ($products = $products->get()). If we want to use the result later in our controller, we could remove it.
Solving BadMethodCallException errors
In the case of your original example, the error BadMethodCallException indicates that Laravel has detected a method call that doesn't exist in its context. Since Eloquent models are chainable, we should ensure that each chained method is applicable to the model instance.
- Make sure the current object is not empty or null before proceeding with any operations:
if ($products) { ... } - Check if the method call is valid for a specific database query. For example,
orderBy()cannot be directly chained after loading all products ($products = Product::all()) because no order is specified during retrieval.
In summary, when working with Laravel Eloquent models, ensure that each method call is well-placed and relevant to the context of your database query. This prevents unexpected errors like BadMethodCallException from occurring in your application's execution.
Conclusion
By understanding Laravel Eloquent's chaining capabilities and correctly executing method calls, you can effectively optimize your database queries. This enables better performance and scalability while maintaining a clearer code structure. Always refer to the official documentation (https://laravelcompany.com/docs) for in-depth explanations on how Eloquent models work.
Note: Remember not to chain get() at the end of your query unless you need to execute and return the result immediately, as it can affect performance. Instead, consider calling get() after you have completed all operations on your models.