Skip to content

Instantly share code, notes, and snippets.

@permatis
Last active March 10, 2016 03:48
Show Gist options
  • Save permatis/01c74f7e6a748c154850 to your computer and use it in GitHub Desktop.
Save permatis/01c74f7e6a748c154850 to your computer and use it in GitHub Desktop.
Create simple helper for get a list all of model in laravel.

How to use

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment