我可以使用User::where()和User::query()->where()
隨著query我在 IDE 中得到提示。但是還有其他好處或指導方針嗎?
uj5u.com熱心網友回復:
除了 IDE 識別查詢構建器實體的啟動之外,沒有其他好處。
由于不使用它沒有任何好處,因為這樣做User::where()會呼叫魔術方法:
// Illuminate\Database\Eloquent\Model
public static function __callStatic($method, $parameters) //$method = "where"
{
return (new static)->$method(...$parameters);
}
哪個呼叫魔術方法
// Illuminate\Database\Eloquent\Model
public function __call($method, $parameters) //$method = "where"
{
if (in_array($method, ['increment', 'decrement'])) {
return $this->$method(...$parameters);
}
if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
return $resolver($this);
}
return $this->forwardCallTo($this->newQuery(), $method, $parameters); //<---- and will end in here
}
雖然User::query()是
// Illuminate\Database\Eloquent\Model
public static function query()
{
return (new static)->newQuery();
}
因此使用User::query()有助于 IDE 并導致更少的步驟,但基本上它是同一件事。(在我們這個時代,兩種方法之間的性能差異可以忽略不計)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492974.html
