Describe your path where models directory, set your namespace model and adding a prefix if you want (optional).
$modelPath = app_path().'\\Models\\';
$namespace = 'App\Models\\';
dd(getAllModels($modelPath, $namespace));
<?php | |
/** | |
* Get a list all of model | |
* @param string $path Get all the files in the destination directory | |
* @param string $namespace Set namespace model | |
* @param string $prefix Variables will be added in the model class suffix | |
* @return $models Get all models with namespace | |
*/ | |
function getAllModels($path, $namespace = 'App\\', $prefix = '') | |
{ | |
$models = array(); | |
foreach(scandir($path) as $file) { | |
if ($file == '.' || $file == '..' ) continue; | |
$models[] = $namespace . str_replace('.php', $prefix, $file); | |
} | |
return $models; | |
} |