This package allows you to find and retrieve all Laravel models in a given folder.
A "model" is any class extending Illuminate\Database\Eloquent\Model.
Here's a quick example:
// On an app containing only the default Laravel "User" model, running:
$models = ModelsFinder::find()
->map(static fn (ModelData $model): array => [
'path' => $model->path,
'class' => $model->class,
])
->toArray();
// would return the following:
[
[
'path' => '/[...]/my-project/app/Models/User.php',
'class' => '\App\Models\User',
],
]You can install the package via composer:
composer require vicgutt/laravel-models-finderYou can initiate the discovery of models by using the find static method.
$models = ModelsFinder::find(
directory: app_path('Models'),
basePath: base_path(),
baseNamespace: '',
);This method accepts 3 optional arguments:
directory: The directory in which to recusively start searching for models. Defaults toapp_path('Models').basePath: The autoloaded entry directory of the project where the search will be initiated. Defaults tobase_path().baseNamespace: The autoloaded base namespace of the project where the search will be initiated. Defaults to''.
The basePath & baseNamespace properties will most likely correspond to an autoloaded entry in a composer.json file.
Example:
Here's an example showcasing searching for a model in the vendor folder:
ModelsFinder::find(
directory: base_path('vendor/spatie/laravel-medialibrary'),
basePath: base_path('vendor/spatie/laravel-medialibrary/src'),
baseNamespace: 'Spatie\MediaLibrary'
)->toArray(),
// would return the following:
[
[
'path' => '[...]/vendor/spatie/laravel-medialibrary/src/MediaCollections/Models/Media.php',
'class' => 'Spatie\MediaLibrary\MediaCollections\Models\Media',
],
]The return value of the method is a lazy collection (Illuminate\Support\LazyCollection).
composer testPlease see CHANGELOG for more information on what has changed recently.
If you're interested in contributing to the project, please read our contributing docs before submitting a pull request.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
{ "autoload": { "psr-4": { // Base namespace | Base path "Spatie\\MediaLibrary\\": "src" } } }