Learning Management Platform for Written Tutorial Series.

Laravel 6 Artisan Console

Laravel 6 Artisan Console

Laravel is a great framework that has most of the features that you need to create applications out of the box. It also comes with a command line utility that you can use to automate a lot of common tasks. Artisan is the console application that Laravel users to automate tasks. You can use it to generate boilerplate code for models, controllers, migrations etc. you can also use it to create the database, seed records etc.

In this tutorial, we will get you up and running with artisan console and provide you with the basics that you need to know.

Topics to be covered

In this tutorial, we will cover the following topics

  • Introduction to Artisan Console
  • Why and When to Use Artisan Console
  • Artisan Common Commands
  • Creating Commands
  • Registering Commands
  • Executing Commands

Introduction to Artisan Console

In the above section, we briefly touched on what artisan is. In this section, we will create a new application and play around with the artisan console to execute commands.

Run the following command to create a new project

composer create-project laravel/laravel artisan 6.0.*

HERE,

  • The above command creates a new Laravel project using latest version of Laravel 6.0

When the project the new project has successfully been created, browse to the root directory using the following command

cd artisan

let's start with a simple command. We will check the version of Laravel that we have installed using artisan.

Run the following command

php artisan --version

 

You should be able to get output similar to the following

Laravel Framework 6.0.1

Let's now see what commands are registered for our application

Run the following command

php artisan list

You should be able to see results similar to the following

Laravel 6 Artisan

If you want to change the default namespace to something else like KodeBlog, then you can run the following command

php artisan app:name KodeBlog

HERE,

  • The above command updates the application namespace from app to KodeBlog in all of the application files.

The general syntax for artisan commands is as follows

php artisan command [--param=value]

HERE,

  • All artisan commands start with php artisan followed by the command name. Some commands all accept parameters with we specify using double dashes followed by the parameter name then we use the equal sign to assign a value to the parameter.

There is a section below that lists some of the most common artisan commands that you will be regularly working with when developing Laravel applications.

Why and When to Use Artisan Console

One of the major benefits of using a framework is improved productivity. Common tasks such as database connections, validation, and other tasks come right out of the box. Laravel artisan console allows us to even double the productivity. Yes, it comes with a built-in controller that we can extend in our own controllers but writing the code that does that takes up time. With artisan console, you can automate that process by running a simple command that accepts the controller name as a parameter and artisan will hand the rest for you. You can use artisan to create various tasks such as;

  • Create controllers
  • Create models
  • Create migrations
  • Create factories
  • Create events
  • Create custom exceptions
  • Create policies
  • Create service providers
  • Create middleware
  • Create validation rules
  • Create artisan commands
  • And many more...

Artisan Common Commands

The following table lists the top 21 artisan commands that every Laravel PHP developer should know

  1. php artisan route:list - this command is used to list all registered Laravel commands
  2. php artisan serve -- starts the built-in web server at the default port of 8000
  3. php artisan make:model - generates the boilerplate code for a database model
  4. php artisan make:controller - generates the boilerplate code for a controller class
  5. php artisan make:migration - generates the boilerplate code for a database migration file
  6. php artisan make:auth - this command is used to scaffold authentication module for the application. The command generates all the necessary routes, controllers and views that are needed to register users, login and reset the password if the user forgets the password.
  7. php artisan make:request - generates the boilerplate code for a Request object. This is usually used for data validation purposes outside the controller.
  8. php artisan make:middleware - generates the boilerplate code for custom middleware that you wish to add to your application. Middleware is a function that is run either before or after a route is executed.
  9. php artisan make:mail - generates the boilerplate code for the mailable class.
  10. php artisan make:notification -- generates the boilerplate code for user notifications.
  11. php artisan make:command - generates the boilerplate code for creating custom commands that can be executed via the artisan console module.
  12. php artisan migrate - executes all the pending database migration files to create the application database.
  13. php artisan migrate:rollback - reverses the executed database migration files.
  14. php artisan migrate:refresh - this command reverses all executed database migration files then executes them again.
  15. php artisan db:seed - executes the database seed files. Seeding is a term that is used to describe the process of adding dummy records to the database for testing purposes.
  16. php artisan list - this command lists all of the registered commands for artisan
  17. php artisan app:name - this command is used to set a custom namespace. By default, the application namespace is set to app. You can change it to reflect your company name as the namespace by using this command.
  18. php artisan vendor:publish - this command is used to publish assets for the third party installed packages. Its usually executed after you install a new package and you need to access resources such as configuration files, assets in the public directory etc.
  19. php artisan down - this command is used to put the application in maintenance mode. This means no page can be accessed on the application. The message that says the application is under maintenance is displayed instead.
  20. php artisan up - this command is the opposite of down. It is used to bring the application back online.
  21. php artisan tinker - this command is used to start the built-in shell program called tinker. Tinker loads the Laravel application environment and allows you to interact with the framework from the command line.

Creating Custom Commands

While Laravel comes with many commands out of the box, we can also create our own custom commands that we can execute from the command line. This section shows you how to create your own command.

Run the following artisan command

php artisan make:command BackupDatabase

HERE,

  • The above command creates a class called BackupDatabase that contains boilerplate code for artisan commands. The new file is created in the directory app/Console/Commands/BackupDatabase.php

Open the file app/Console/Commands/BackupDatabase.php and update the code to the following

<?php

namespace KodeBlog\Console\Commands;

use Illuminate\Console\Command;

class BackupDatabase extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'kb:backupdb';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Make a backup of the application database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //Business logic for making database backup goes here
        $this->info("Congratulations, application database has successfully been created and uploaded to cloud server.");
    }
}

HERE,

  • class BackupDatabase extends Command{...} extends the parent class Command which comes with Laravel framework.
  • protected $signature = 'kb:backupdb'; defines the name of the command that will be used to execute it.
  • protected $description = 'Make a backup of the application database'; sets the description text of the command. This is displayed when you run php artisan list
  • public function handle() this method contains the business logic of the command. That is to say performs the task that we want our command to do.
  • $this->info("Congratulations, application database has successfully been created and uploaded to cloud server."); this line is used to provide feedback to the user after the command has been executed successfully.

Let's now check if our custom command has been registered.

Run the following command

php artisan list

You should be able to see our custom command as shown in the image below

Laravel Artisan Custom Command

Let's now execute our command

php artisan kb:backupdb

You should be able to get the following results

Artisan execute custom command

As you can see from the above image, our application has displayed the information that we set to display after it executes successfully.

Summary

In this tutorial, we introduced you to Laravel artisan the built-in command line utility that enhances your productivity when developing Laravel applications. We also covered the commonly used artisan commands and we were able to create our own custom command that we can also execute from the command line.

What next?

If you enjoyed this lesson then show us your appreciation by creating a free accounts on our site. As always we appreciate your comments down below.


...