I want to know that, what is difference between Eloquent's Model::query()->get() and Model::get()?
Model::get();
and
Model::query()->get();
I want to know that, what is the benefit to use query() method?
I want to know that, what is difference between Eloquent's Model::query()->get() and Model::get()?
Model::get();
and
Model::query()->get();
I want to know that, what is the benefit to use query() method?
Model::get()
will call for the magic method on Illuminate\Database\Eloquent\Model
public static function __callStatic($method, $parameters)
which will create a new instance and call the magic method
public function __call($method, $parameters)
which will return
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
And then the ->get()
method will be called on the resulting Query builder instance
While Model::query()
will call
public static function query()
{
return (new static)->newQuery();
}
and return a query builder instance skipping the magic method steps.
The big difference is that Model::query()
is statically declared and more IDE friendly so type hinting will work correctly with it.