1

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?

N69S
  • 16,110
  • 3
  • 22
  • 36
AR-Shahin
  • 11
  • 2
  • 1
    It's the same thing, query() will return a new query builder – Lk77 Nov 24 '22 at 13:24
  • to explain further, when you call ::get() statically, laravel will internally call query() for you, and forward your get() call to the query builder, so `Model::get() == Model::query()->get()` – Lk77 Nov 24 '22 at 13:34
  • Does this answer your question? [What is the meaning of Eloquent's Model::query()?](https://stackoverflow.com/questions/51517203/what-is-the-meaning-of-eloquents-modelquery) – steven7mwesigwa Nov 24 '22 at 13:55

1 Answers1

2

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.

N69S
  • 16,110
  • 3
  • 22
  • 36