Laravel Custom Model Methods
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Eloquent models in Laravel provide a convenient way to interact with your database through the ActiveRecord pattern. However, when it comes to adding custom logic and functionality to your model's methods, some developers feel the need to create static methods instead of non-static ones to call them using the model's facade. This leads to less than ideal solutions, as they are less flexible and more tightly coupled with specific data structures.
The Issue: Non-static Method Calling
In Laravel, non-static methods of a class can be called using the model instance. However, when you try to call these non-static functions from outside the class using the model's facade, an error occurs due to the use of static calls, which are not compatible with non-static methods.
The Solution: Magic Method __call
Laravel handles this issue by providing a built-in magic method called __call(). This method allows you to define custom functions that can be triggered using your model's facade. Here's the syntax for using it:
public function __call($method, $parameters) {
// Process and return resulting array (or any other structure)
return array('...');
}
Usage Example
Let's revisit our previous example: you have a model called Car, and you want to get all cars sorted by make. You could create a custom function within the Car model that uses the __call() magic method as follows:
public function __call($method, $parameters) {
if ($method === 'getAllSortedByMake') {
// Process and return resulting array
return Car::all()->groupBy('make');
}
}
Now, from your controller or any other place where you need to access this functionality, simply call the model's facade as follows:
$cars = Car::getAllSortedByMake();
Advantages of the __call() Magic Method
- It allows you to add custom logic outside the model without using static methods, which improves flexibility and readability.
- It enables you to use your models in any context, enhancing their usability across the application.
- It reduces coupling between your data structures and specific algorithms or calculations, making them more reusable and testable.
Conclusion
By leveraging the __call() magic method in Laravel models, you can add custom logic to your models without creating static methods, allowing for a more streamlined and flexible approach. This makes it easier to utilize your models across different parts of your application while maintaining better code organization and readability.
P.S.: Remember that this solution depends on the context in which you want to use your model. It may not be the best fit for every situation, so always consider the trade-offs between different approaches before making a decision.