Laravel Magazine

Model Pruning in Laravel: The Custom Path Solution

Eric Van Johnson · Laravel Tutorials
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.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.