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

Rretzko's avatar
Level 15

Class "/App/Models/User" not found

Hi All - I think this is more of a php problem than Laravel, but... I have a Service who's job it is to return the name, path, and row count for all models found under the /App/Models directory. The Service sits in the /App/Services/ directory. The service is working fine except for one line:

$rowCount = $modelPath::get()->count();

The enclosing method is:

private function parseNameAndPath(string $model): array //ex: C:/xampp/htdocs/dev/tdr2024/app/Models/User
    {
		//split the incoming string into parts
		//ex [C:, xampp, htdocs, dev, tdr2024, app/Models/User]
        $localParts = explode('\', $model); 

		//isolate the /App/Models/ tail
		//ex. /App/Models/User
        $modelPath = '/' . ucfirst($localParts[(count($localParts) - 1)]); 

		//isolate the model name
        $modelPathParts = explode('/', $modelPath);

		//ex. User
        $modelName = $modelPathParts[(count($modelPathParts) - 1)]; 
		
		//ex. U
        $modelInitials = $this->modelInitials($modelName); 

		//THIS IS WHERE THE "CLASS NOT FOUND" ERROR OCCURS
        $rowCount = $modelPath::get()->count();

        return [
            'name' => $modelName,
            'initials' => $modelInitials,
            'path' => $modelPath,
            'rowCount' => $rowCount,
        ];
    }

I know there's an \App\Models\User class and that it has rows. If I dd(\App\Models\User::get()->count()), it returns the correct value. I'm guessing that I can't call the model directly from a variable, but haven't figured out why that's causing a problem or how to solve it. I've tried putting curly brackets around {$modelPath}::get()->count(), but I get the same error. Your suggestions are appreciated!

0 likes
8 replies
LaryAI's avatar
Level 58

The issue is that $modelPath is a string containing the path to the model, not the actual model class. To get the model class, you can use the app() function to resolve the class from the path. Here's an updated version of the parseNameAndPath() method:

private function parseNameAndPath(string $model): array
{
    $localParts = explode('\', $model);
    $modelPath = '\' . ucfirst($localParts[(count($localParts) - 1)]);
    $modelName = basename($modelPath, '.php');
    $modelInitials = $this->modelInitials($modelName);

    $modelClass = app()->getNamespace() . 'Models' . str_replace('/', '\', dirname($modelPath)) . '\' . $modelName;

    $rowCount = $modelClass::get()->count();

    return [
        'name' => $modelName,
        'initials' => $modelInitials,
        'path' => $modelPath,
        'rowCount' => $rowCount,
    ];
}

This code uses the app()->getNamespace() method to get the namespace of the application, and then constructs the full class name by appending the namespace, the Models directory, the directory path of the model, and the model name. The str_replace() function is used to convert the forward slashes in the path to backslashes, which is the namespace separator in PHP.

Note that this assumes that all models are located in the app/Models directory. If your models are located in a different directory, you'll need to adjust the code accordingly.

Albert90's avatar

@LaryAI And str_replace() needs to escape the backslashes... Don't forget about that.

kokoshneta's avatar
Level 27

Class names (well, namespaces) use backslashes, not forward slashes. /App/Models/User is not an existing class name; \App\Models\User is.

You also don’t need to ->get() all the results – that will return every row in the database and load them all into memory for nothing. Just do $model::count().

Also, why are you passing a string to the function and exploding that? If you know the point is to look through the models in the app/Models folder, why not just iterate through that via the filesystem?

Snapey's avatar

probably simpler just to ask the database for all table names and row count

kokoshneta's avatar

@Snapey As long as there aren’t any non-model tables in the database that would need to be filtered out (which I guess would lead back to iterating through the Models directory). Don’t forget that pivot tables would need to be excluded, for example.

newbie360's avatar

@rretzko actually what are you trying to do, i'm confusing ;)

put this two method into any model, or use Traits

    public function getModelNamespace()
    {
        return '\\' . get_class($this); // ex: \App\Models\User
    }

    public function getModelName()
    {
        return class_basename($this); // ex: User
    }
// ex: User
$model = @end(
    explode(DIRECTORY_SEPARATOR, 'C:/xampp/htdocs/dev/tdr2024/app/Models/User')
);

$modelNamespace = "\App\Models\{$model}";
$model = new $modelNamespace;
$rowCount = 0;

if (method_exists($model), 'getModelNamespace')) {
    $rowCount = $modelNamespace::count();
}
Rretzko's avatar
Level 15

Thanks @laryai @kokoshneta @snapey @albert90 @newbie360 for your recommendations and suggestions!

$modelClass = app()->getNamespace() . 'Models' . '\' . $modelName;

got the job done and your additional suggestions gave me a better model overall. This is a great community and your quick responses are appreciated!

Please or to participate in this conversation.