TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

How to Create Model in Laravel

Laravel comes with a handy artisan command to create a model for your application. In this post, we take a look at various ways to create a model class using the artisan command.

Create a model class

To create only a model class, run the artisan command below:

php artisan make:model Photo

You will get a model class file at app/Models/Photo.php.

Create a pivot model class

To create only a pivot model class, run the artisan command below:

php artisan make:model Photo --pivot

You will get a model class file at app/Models/Photo.php.

Create a model class with a migration

To create only a model class with a migration, run the artisan command below:

php artisan make:model Photo -m 

You will get a model class file at app/Models/Photo.php along with a migration file in database/migrations/ folder.

Create a model class with a factory class

To create only a model class with a factory, run the artisan command below:

php artisan make:model Photo -f

You will get a model class file at app/Models/Photo.php along with a factory class at database/factories/PhotoFactory.php.

Create a model class with a seeder class

To create only a model class with a seeder, run the artisan command below:

php artisan make:model Photo -s

You will get a model class file at app/Models/Photo.php along with a seeder class at database/seeders/PhotoSeeder.php.

Create a model class with a controller class

To create only a model class with a controller, run the artisan command below:

php artisan make:model Photo -c

You will get a model class file at app/Models/Photo.php along with a controller class at app/Http/Controllers/PhotoController.php.

Create a model class with a policy class

To create only a model class with a policy, run the artisan command below:

php artisan make:model Photo --policy

You will get a model class file at app/Models/Photo.php along with a policy class at app/Policies/PhotoPolicy.php.

The End

You can certainly combine multiple options when creating a model class, for example, if you wish to create a model with a seeder and a migration, you can do so by running the command below:

php artisan make:model Photo -s -m