January 22nd, 2024

Model Pruning in Laravel: The Custom Path Solution

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:

1php artisan model:prune --path="CustomModelPath"

By default, the --path option remains set to Models, ensuring backward compatibility and not disrupting existing applications.

Statamic Ninja


Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

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.