Answer the question
In order to leave comments, you need to log in
How to get list of all models in laravel?
Hello. The task is to get an array of all created models.
Maybe in laravel there is such a method that returns a list of all existing models in the project?
If not, then for example, if the models are located directly in the app/ folder, then how to iterate through all the files with a capital letter in the app folder, without going into the subdirectory? Or maybe it's possible to sort through all the classes that are direct descendants for App?
Thank you all in advance.
Answer the question
In order to leave comments, you need to log in
1)
We get a list of files using scandir
Then we filter, for example, through preg-match
2) Through the use of external programs
$row = exec('ls -ls',$output,$error);
while(list(,$row) = each($output)){
echo $row, "<BR>\n";
}
if($error){
echo "Error : $error<BR>\n";
exit;
}
$dir = '/path/to/model/directory';
$files = scandir($dir);
$models = array();
$namespace = 'Your\\Model\\Namespace\\';
foreach($files as $file) {
//skip current and parent folder entries and non-php files
if ($file == '.' || $file == '..' || !preg_match('\.php', $file)) continue;
$models[] = $namespace . preg_replace('\.php$', '', $file);
}
print_r($models);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question