-
I am trying to create a select dropdown for a Taxonomy Model, So I can assign taxonomies and related tags to specified Models I am not sure how to have the system find all models minus Taxonomy, display the name, and store the full path to the model in the database. IE the dropdown will display USER but store Modules\Users\Models\User in the database I am trying to build this in Filament select form if that makes a difference |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Here's a way to dynamically get all models from all enabled modules $modules = Module::allEnabled();
$models = [];
foreach ($modules as $module) {
$modelsPath = Module::getModulePath($module->getName()) . config('modules.paths.generator.model.path');
$modulesDirectoryName = basename(config('modules.paths.modules'));
if (File::exists($modelsPath)) {
// Scan for all PHP files in the Models directory
$files = File::glob("{$modelsPath}/*.php");
foreach ($files as $file) {
$relativePath = substr($file, strpos($file, $modulesDirectoryName));
$filename = basename($file); // Extract the filename
$models[$relativePath] = $filename;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
If you are happy to hard code you can do instead: $modelsPath = Module::getModulePath($module->getName()).'/app/Models';
$modulesDirectoryName = 'Modules'; |
Beta Was this translation helpful? Give feedback.
-
Slightly better version $modules = Module::allEnabled();
$models = [];
$modulesDirectoryName = basename(config('modules.paths.modules')); // Compute once
foreach ($modules as $module) {
$modelsPath = Module::getModulePath($module->getName()) . config('modules.paths.generator.model.path');
if (!File::exists($modelsPath)) {
continue; // Skip if the directory doesn't exist
}
// Scan for all PHP files in the Models directory
$files = File::glob("{$modelsPath}/*.php");
foreach ($files as $file) {
$relativePath = substr($file, strpos($file, $modulesDirectoryName));
$filename = basename($file); // Extract the filename
// Add model with relative path as key and filename as value
$models[$relativePath] = $filename;
}
} |
Beta Was this translation helpful? Give feedback.
Slightly better version