E
E
Ernest Faizullin2016-01-21 13:41:39
Laravel
Ernest Faizullin, 2016-01-21 13:41:39

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 answer(s)
L
LightAir, 2016-01-21
@erniesto77

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;
}

Then we filter, for example, through preg-match
3) Manually compose a list and put it in json || db || xml || txt etc.
PS
Here is an example by the way from stackoverflow.com/questions/31837075/laravel-get-l...
$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);

PPS You can also get files using any library. For example, with finder you don’t have to make a regular expression, it will be enough to check the class instance for the necessary match using instanceof.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question