How to create Laravel custom artisan command and run it from the terminal
You can create you own artisan command in Laravel and use it anytime from the terminal. In this topic, we will see how you can create Laravel custom Command and run it from the command line. You can use Laravel create command to create your new command and use it as an artisan command.
Step1 - Create a Laravel project
Let us create a project in laravel 10. Make sure you have composer and PHP installed in your system. I have given the project name as lara_command.
This will create a folder named "lara_command" under the folder where you run the above command.
Step 2 - MySQL Database setup and Migration
Update .env
file for database details. I am using MySQL database named 'lara_command', so I updated the file as below:
We will create a new table 'trainings', so we need to create a Laravel migration for the new table. This table will store the details of all courses. Go to your project folder and run below command from VS code terminal to create model, controller and migration.
Above command will create the model, controller and the migration file. I am using VS code and three files are marked below:
Migration for the custom table
Let us update the Laravel migration file for the table 'trainings'.
Let us now run the migration to create the default Laravel tables and our custom table using php artisan migrate
command. Run below artisan command from project root to create the tables.
This will create all the default Laravel tables as well as our custom 'trainings' table. After migration see the structure of 'trainings' table.
Let us update the model.
Step 3 - Controller
We will create a method createCourse()
in TrainingController to insert a row in 'trainings' table.
We are just creating a simple method to insert a row in Training model to show how you can call controller method from command line.
As this is for testing purpose, I am hard coding the values for the fields. All I want is to show that controller method run successfully from the command line. In actual method you might have to do many other processing like, selecting from other tables, doing some processing and then finally update or insert into a table.
Step 4 - Create Laravel Command
Now, we will create a Laravel command, go to VS code terminal and run below command to create a Laravel command from project root.
Command file "CreateCourseCommand.php"
will be created in app/Console/Commands folder as shown below:
Let us now update this command to call controller method:
app/Console/Commands/CreateCourseCommand.php
Our development is done. We have created our Laravel custom artisan command to call the controller creatreCourse()
method. If we run the command it should insert a row in 'trainings' table. Let us test it now.
Step 5 - Test the application
Check the table in MySQL, if there is any data already in the 'trainings' table. For my testing I kept the 'trainings' table empty.
From the terminal enter below command from project root:
It should give a message as "course created". Check the table, a row should be inserted.
Download Source code from github.