Model Pruning in Laravel: The Custom Path Solution
The model pruning command by default seeks models in the app/Models directory. However, what if your Laravel application deviates from this norm, storing models in a different directory?
The problem
If your models are located in the app directory and not app/Models, running the php artisan model:prune command will trigger a DirectoryNotFoundException exception. Although Laravel offers a --model option to specify individual models, this becomes impractical with multiple models.
Introducing the --path option
To address this, Davo proposed a more flexible approach in his PR that was included in the latest Laravel release. The PR is introducing a --path option in the model:prune command. This addition allows you to specify your desired application path for model pruning, enhancing customization and convenience.
Here's how you can use this new feature:
php artisan model:prune --path="CustomModelPath"
By default, the --path option remains set to Models, ensuring backward compatibility and not disrupting existing applications.