Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FrenchFryNinja's avatar

Getting all table rows as a query builder?

I know about ModelName::all(), but that returns a collection. I need to have all of the results returned as a query builder, but can't for the life of me find out how, other than to write something like:

ModelName::where('id', '>=' 0);

Is this the appropriate way to go about it?

EDIT for clarity:

Essentially I'm not sure I knew what I was asking. The end result was a needed a way to get a builder object back with optional user provided parameters, so I could later call ->get() or->paginate(x) on it as needed. Apparently I missed the basics of query builders. Calling Model::query() gets what I needed.

0 likes
3 replies
Cronix's avatar

I need to have all of the results returned as a query builder

That doesn't make sense. Once you execute the query, it's no longer a "query builder" object. It's the results of whatever query you executed.

Maybe explain what you're trying to accomplish?

jlrdw's avatar

So curious on the question.

I know there is the query builder. But what is results returned as a query builder?

I never heard of that. Do you mean as an array?

tykus's avatar
tykus
Best Answer
Level 104

You get an Eloquent Builder instance by calling:

Model::query()

You can assign to a variable and continue chaining as you please.

I'm not sure what you mean by getting all table rows; your query builder instance can be assumed to begin with all rows...

1 like

Please or to participate in this conversation.