Laravel自定义模型方法 [英] Laravel Custom Model Methods
问题描述
每当我向Eloquent模型添加其他逻辑时,我最终都不得不使其成为static
方法(即不理想),以便从模型的外观调用它.我已经尝试了很多有关如何正确执行此操作的搜索,并且几乎所有结果都涉及创建返回Query Builder接口部分的方法.我试图弄清楚如何添加可以返回任何内容并使用模型的外观调用的方法.
Whenever I add additional logic to Eloquent models, I end up having to make it a static
method (i.e. less than ideal) in order to call it from the model's facade. I've tried searching a lot on how to do this the proper way and pretty much all results talk about creating methods that return portions of a Query Builder interface. I'm trying to figure out how to add methods that can return anything and be called using the model's facade.
例如,假设我有一个名为Car
的模型,并希望将它们全部获取:
For example, lets say I have a model called Car
and want to get them all:
$cars = Car::all();
太好了,除了现在,假设我要通过make将结果排序到多维数组中,这样我的结果可能看起来像这样:
Great, except for now, let's say I want to sort the result into a multidimensional array by make so my result may look like this:
$cars = array(
'Ford' => array(
'F-150' => '...',
'Escape' => '...',
),
'Honda' => array(
'Accord' => '...',
'Civic' => '...',
),
);
以那个理论示例为例,我很想创建一种可以像这样调用的方法:
Taking that theoretical example, I am tempted to create a method that can be called like:
$cars = Car::getAllSortedByMake();
片刻,让我们忘记可怕的方法名称以及它与数据结构紧密耦合的事实.如果我在模型中制作了这样的方法:
For a moment, lets forget the terrible method name and the fact that it is tightly coupled to the data structure. If I make a method like this in the model:
public function getAllSortedByMake()
{
// Process and return resulting array
return array('...');
}
最后在我的控制器中调用它,我将抛出此异常:
And finally call it in my controller, I will get this Exception thrown:
假设$ this来自不兼容的上下文,则不应静态调用非静态方法Car :: getAllSortedByMake()
Non-static method Car::getAllSortedByMake() should not be called statically, assuming $this from incompatible context
TL; DR :如何在不使其成为静态方法的情况下添加有意义的自定义功能,而无需使用模型的外观对其进行调用?
TL;DR: How can I add custom functionality that makes sense to be in the model without making it a static method and call it using the model's facade?
这是一个理论示例.改写这个问题可能更有意义.为什么在Eloquent模型的外观上可以使用某些非静态方法(例如all()
或which()
),而不能在模型中添加其他方法?这意味着正在使用__call
魔术方法,但是如何使它识别模型中的我自己的函数?
This is a theoretical example. Perhaps a rephrase of the question would make more sense. Why are certain non-static methods such as all()
or which()
available on the facade of an Eloquent model, but not additional methods added into the model? This means that the __call
magic method is being used, but how can I make it recognize my own functions in the model?
关于排序"的一个更好的例子是,如果我需要对一条数据运行计算或算法:
Probably a better example over the "sorting" is if I needed to run an calculation or algorithm on a piece of data:
$validSPG = Chemical::isValidSpecificGravity(-1.43);
对我来说,像这样的东西在模型中是有意义的,因为它是特定于领域的.
To me, it makes sense for something like that to be in the model as it is domain specific.
推荐答案
我的问题更多是在基本层面上,例如为什么all() 可通过立面访问?
My question is at more of a fundamental level such as why is all() accessible via the facade?
如果您查看Laravel核心-all()实际上是一个静态函数
If you look at the Laravel Core - all() is actually a static function
public static function all($columns = array('*'))
您有两个选择:
public static function getAllSortedByMake()
{
return Car::where('....')->get();
}
或
public function scopeGetAllSortedByMake($query)
{
return $query->where('...')->get();
}
两者都可以让你做
Car::getAllSortedByMake();
这篇关于Laravel自定义模型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!