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

jd_itsme's avatar

Get model's class name from eloquent collection even if the query does not return any results

Say there are no users in the system yet and I am running this query:

$users = User:all();

How do I resolve the model's class name from the $users collection even if it does not return any results?

0 likes
5 replies
topvillas's avatar

You can't, there is no collection. But you already know the model name.

jd_itsme's avatar

There is a collection, it is just empty.

topvillas's avatar

Yeah, you're right. But you still can't get the model name because there are no models.

jd_itsme's avatar

:( They could have stored this information somewhere on the model

Please or to participate in this conversation.