// oneliner
dd(collect(glob(app_path('Models') . '/*.php'))->map(fn ($file) => basename($file, '.php'))->toArray());
<?php
// found link from https://stackoverflow.com/questions/34053585/how-do-i-get-a-list-of-all-models-in-laravel
// and then finally found on https://gist.github.com/mohammad425/231242958edb640601108bdea7bcf9ac
function getAllModels(): array
{
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
$models = [];
foreach ((array)data_get($composer, 'autoload.psr-4') as $namespace => $path) {
$models = array_merge(collect(File::allFiles(base_path($path)))
->map(function ($item) use ($namespace) {
$path = $item->getRelativePathName();
return sprintf('\%s%s',
$namespace,
strtr(substr($path, 0, strrpos($path, '.')), '/', ''));
})
->filter(function ($class) {
$valid = false;
if (class_exists($class)) {
$reflection = new ReflectionClass($class);
$valid = $reflection->isSubclassOf(IlluminateDatabaseEloquentModel::class) &&
!$reflection->isAbstract();
}
return $valid;
})
->values()
->toArray(), $models);
}
return $models;
}