laravel 雄辩的第一个 where 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33211659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent the first where clause
提问by Yada
I am wondering how Laravel implement eloquent syntax so that the first where clause can be called statically with User::where()
我想知道 Laravel 如何实现 eloquent 语法,以便可以静态调用第一个 where 子句 User::where()
User::where('id', 23)->where('email', $email)->first();
Do they have a public static function where()and a public function where()
他们有一个public static function where()和一个public function where()
回答by Amo
Calling whereon an Eloquent model does involve a little bit of magic that occurs behind the scenes. Firstly, take the example of:
调用whereEloquent 模型确实涉及到一些幕后发生的魔法。首先,举个例子:
User::where('name', ‘Joe')->first;
There's no static wheremethod that exists on the Modelclass that the Userclass extends.
类扩展的类where上不存在静态方法。ModelUser
What happens, is that the PHP magic method __callStaticis called which attempts to then call the wheremethod.
会发生什么,是__callStatic调用PHP 魔术方法,然后尝试调用该where方法。
public static function __callStatic($method, $parameters)
{
$instance = new static;
return call_user_func_array([$instance, $method], $parameters);
}
As there's no explicitly defined user function called where, the next magic PHP method __callwhich is defined in Modelis executed.
由于没有显式定义的用户函数被调用where,下一个__call在Model中定义的魔法 PHP 方法被执行。
public function __call($method, $parameters)
{
if (in_array($method, ['increment', 'decrement'])) {
return call_user_func_array([$this, $method], $parameters);
}
$query = $this->newQuery();
return call_user_func_array([$query, $method], $parameters);
}
The common database related methods become accessible via:
常见的数据库相关方法可通过以下方式访问:
$query = $this->newQuery();
This instantiates a new Eloquent query builder object, and it's on this object that the wheremethod runs.
这会实例化一个新的 Eloquent 查询构建器对象,并且该where方法运行在这个对象上。
So, when you use ```User::where()`` you're actually using:
因此,当您使用 ```User::where()`` 时,您实际上是在使用:
Illuminate\Database\Eloquent\Builder::where()
Take a look at the Builder classto see all of the common Eloquent methods you're used to using, like where(), get(), first(), update(), etc.
看看在生成器类,看看大家的雄辩方法,你习惯使用,比如where(),get(),first(),update(),等。
Laracastshas a great in-depth (paid) video on how Eloquent works behind the scenes, which I recommend.
Laracasts有一个关于 Eloquent如何在幕后工作的深入(付费)视频,我推荐它。
回答by Daan
Well let's find out.
好吧,让我们来了解一下。
When we open a model it extends Modelso let's open that class.
In the class Modelwe find 2 "magic" methods called __call()and __callStatic()
当我们打开一个模型时,它会扩展,Model所以让我们打开那个类。在类中,Model我们找到了 2 个“神奇”的方法,称为__call()和__callStatic()
__call()is triggered when invoking inaccessible methods in an object context.
__call()在对象上下文中调用不可访问的方法时触发。
__callStatic()is triggered when invoking inaccessible methods in a static context.
__callStatic()在静态上下文中调用不可访问的方法时触发。
We also see in class Modelit makes use of the class use Illuminate\Database\Query\Builder as QueryBuilder;
我们也在课堂上看到Model它使用了类use Illuminate\Database\Query\Builder as QueryBuilder;
If we open the Builderclass we find a method called public function where()
如果我们打开这个Builder类,我们会发现一个名为public function where()
So if you call User::whereit calls __callStatic('where', $parameters)from the Modelclass.
因此,如果您调用User::where它,__callStatic('where', $parameters)则从Model类调用它。
I hope this makes sense.
我希望这是有道理的。

