Laravel 5.4 #23 : Commands. Create an installer command for the project.

We are going to create a command with Laravel that will allow us to leave our project ready to use simply with an artisan command.

First, we have to create the file, as usually, Laravel provide us an artisan command to do it:

Open the file and put this code:

  • The variable $signature indicates the artisan command that we must set to execute.
  • The handle function is what the command executes (cleans cache, asks for data, inserts them in .env, executes migrations and seeds, executes npm and compiles assets).
  • The changeEnv function I got it from and what it does is changing the indicated variables in the .env file. I have add one line to detect if the .env file exists, if not it takes .env.example.


As I said, in my case, I have named it blog: installer, so we must run:

Simply with this command, it should install and execute everything you need (db, publish configs, compilation assets, etc.). Obviously, the code can be improved a lot, but this entry is simply an example of how it could be done.

Remember that before executing the command you must create the database.