diff --git a/package.json b/package.json index a4f3bd7a..d9daf69b 100644 --- a/package.json +++ b/package.json @@ -1064,6 +1064,13 @@ "default": true, "description": "Automatically generate Eloquent doc blocks for models as IDE helpers." }, + "Laravel.eloquent.modelPaths": { + "type": "array", + "default": [ + "app/Models" + ], + "description": "Directory paths or glob patterns for model files, relative to the project root." + }, "Laravel.pest.generateDocBlocks": { "type": "boolean", "default": true, diff --git a/php-templates/models.php b/php-templates/models.php index c8c43faa..c8f529aa 100644 --- a/php-templates/models.php +++ b/php-templates/models.php @@ -54,11 +54,16 @@ public function __construct(protected $factory) public function all() { - if (\Illuminate\Support\Facades\File::isDirectory(base_path('app/Models'))) { - collect(\Illuminate\Support\Facades\File::allFiles(base_path('app/Models'))) - ->filter(fn(\Symfony\Component\Finder\SplFileInfo $file) => $file->getExtension() === 'php') - ->each(fn($file) => include_once($file)); - } + collect(__VSCODE_LARAVEL_MODEL_PATHS__) + ->flatMap(function ($path) { + try { + return \Illuminate\Support\Facades\File::allFiles(base_path($path)); + } catch (\Symfony\Component\Finder\Exception\DirectoryNotFoundException) { + return []; + } + }) + ->filter(fn(\Symfony\Component\Finder\SplFileInfo $file) => $file->getExtension() === 'php') + ->each(fn($file) => include_once($file)); return collect(get_declared_classes()) ->filter(fn($class) => is_subclass_of($class, \Illuminate\Database\Eloquent\Model::class)) diff --git a/src/repositories/models.ts b/src/repositories/models.ts index 000c3426..9d32f3d4 100644 --- a/src/repositories/models.ts +++ b/src/repositories/models.ts @@ -3,8 +3,9 @@ import { Eloquent } from ".."; import { writeEloquentDocBlocks } from "../support/docblocks"; import { runInLaravel, template } from "./../support/php"; import { escapeNamespace } from "../support/util"; +import { config } from "@src/support/config"; -const modelPaths = ["app", "app/Models"]; +const modelPaths = config("eloquent.modelPaths", ["app/Models"]); const load = () => { return runInLaravel( diff --git a/src/support/config.ts b/src/support/config.ts index 54dface7..75279d57 100644 --- a/src/support/config.ts +++ b/src/support/config.ts @@ -12,6 +12,7 @@ type ConfigKey = | "showErrorPopups" | "blade.autoSpaceTags" | "eloquent.generateDocBlocks" + | "eloquent.modelPaths" | "pest.generateDocBlocks" | "pest.helperFilePath" | "env.viteQuickFix" diff --git a/src/templates/models.ts b/src/templates/models.ts index 0ddaf9e2..a92d2878 100644 --- a/src/templates/models.ts +++ b/src/templates/models.ts @@ -54,11 +54,16 @@ $models = new class($factory) { public function all() { - if (\\Illuminate\\Support\\Facades\\File::isDirectory(base_path('app/Models'))) { - collect(\\Illuminate\\Support\\Facades\\File::allFiles(base_path('app/Models'))) - ->filter(fn(\\Symfony\\Component\\Finder\\SplFileInfo $file) => $file->getExtension() === 'php') - ->each(fn($file) => include_once($file)); - } + collect(__VSCODE_LARAVEL_MODEL_PATHS__) + ->flatMap(function ($path) { + try { + return \\Illuminate\\Support\\Facades\\File::allFiles(base_path($path)); + } catch (\\Symfony\\Component\\Finder\\Exception\\DirectoryNotFoundException) { + return []; + } + }) + ->filter(fn(\\Symfony\\Component\\Finder\\SplFileInfo $file) => $file->getExtension() === 'php') + ->each(fn($file) => include_once($file)); return collect(get_declared_classes()) ->filter(fn($class) => is_subclass_of($class, \\Illuminate\\Database\\Eloquent\\Model::class))