Remember this artisan command when you create a new table

Remember this artisan command when you create a new table


2 min read

Hey guys,

This command has been presented there for a while but I want to share it again ๐Ÿฅน

Before I usually do:

  • php artisan make:migration CreateAbcTables

  • php artisan make:model Abc

  • php artisan make:factory Abc

  • ...

It's quite a hassle, isn't it? But actually, there is a way to do it in 1 go ๐Ÿ˜Ž

The combination

php artisan make:model Article --migration --factory

Once hitting this one, it will create for us:

  • Article model

  • Migration (with Schema::create('articles', ...)), automatically do the "pluralize".

  • Article Factory (optional - if you want to write tests)

Additional params

  • --controller

  • --resource

  • --policy

  • --seed

  • --all: this will create everything ๐Ÿ˜†

IMO, I do think that model, migration & factory are good enough for the first go. We can create a controller or policy or else in later ๐Ÿค—.

Additional tips

When creating a migration to change the schema of an existing table, try put the name like this:

  • AddUserIdColumnToUsersTable

  • ChangeAmountTypeInTransactionsTable

  • DeleteUserIdFromEventsTable

Laravel will extract the middle part, transform the table into the snack_case, and prepare the base code for us

Schema::table('{your_table}', ...);

Save a bit of your time, eh? ๐Ÿฅน

Ending words

Thank you for reading this, I believe many of us already know & aware of these options. Save time and jump into productivity quicker.

Laravel is so beautiful ๐Ÿ˜