Skip to content
Advertisement

Laravel CRUD API Fatal Error: Trait ‘IlluminateDatabaseEloquentFactoriesHasFactory’ not found

I was following this tutorial, but with my own dataset: https://www.positronx.io/php-laravel-crud-operations-mysql-tutorial

And I got stuck on the phase of Creating and storing data.

After filling all the fields:gui

Im getting this error:hasfactory error

When I comment use HasFactory it’s showing this commented hasfactory

Please help!

Advertisement

Answer

The HasFactory trait wasn’t introduced until Laravel 8x. That’s also when they moved all the models into the app/Models directory.

I’m guessing that you’re on Laravel 7x (or earlier), which does not have the HasFactory trait, and where all the models are stored in the app directory by default.

If you want to follow that tutorial, you’ll want to start with a Laravel 8x application. If you want to stay on 7x, you’ll need to adjust some things to work in 7x. For example, delete the use statement for the HasFactory trait. Additionally, your models are probably in the App namespace, not the AppModels namespace.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement