Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Setup Laravel 5.7 Application in Just 1-click on Different Cloud Providers Like GCE , AWS , DO , Linode , Vultr Deploy Now!

Creating Custom Composer Commands with PHP Artisan in Laravel

Updated on March 6, 2019

4 Min Read
php artisan
Reading Time: 4 minutes

Artisan is a command line utility that comes with Laravel, one of the most popular PHP frameworks.This utility is extensively used for setting up migrations, routes listings, queueing, class creations, and other related tasks.

For a complete list of tasks that Artisan could help with, run the following command inside the Laravel project directory:

public_html $>  php artisan
Laravel Framework version 5.3.19
...
...

Make is an important command in Artisan, used for creating custom Artisan commands.

make
 make:auth        	Scaffold basic login and registration views and routes
 make:command     	Create a new Artisan command

If you have a task which often pops up during your projects, create an Artisan command, add the code into the class, and fire away from the CLI. Optionally, add the Artisan command as a cron job.

What is PHP Artisan?

If you haven’t worked with Laravel, then you might not be familiar with Laravel artisan commands. Developers use PHP Artisan to perform some important functional operations which include generating migrations, publishing package assets and many similar tasks. You can not only work with built-in Laravel commands in Artisan, but can also work with your own custom Laravel commands as well.

Powerful Laravel Hosting For All Your Needs

Grow your Laravel app with our feature-rich managed hosting

There is a whole list of pre-built Laravel artisan commands you can use to your advantage, but many developers like to create their own commands according to their project needs. To check out the pre-built PHP Laravel artisan commands, just type the following command, and you will get the complete list of Laravel artisan commands list on screen.

php artisan list

You will get the complete list of built-in PHP Laravel artisan commands. All of these commands help developers do their work with more efficiency, thereby saving them their precious time. Using these Laravel artisan commands, you can create auth, controller, model, mail, migration and many other functions.

Laravel Artisan commands foundation

Laravel 3.0 was quite basic in its code structure, but it still had some excellent features to offer. With Laravel 5.7 release, you get some existing packages developed by other Laravel developers as well. Therefore, Laravel 4 mainly depends on the packages from Symfony framework.   

Looking closely at the source of Artisan application in Illuminate\Console\Application directory, you will notice that the class itself extends Symfony\Component\Console\Application.

Although Artisan uses the Symfony’s most known console component, it still has some common methods named with Laravel-like aliases. Hence, being a developer, you will still feel developing natively with Laravel.

Laravel PHP artisan server

The Laravel PHP artisan serve command helps running applications on the PHP development server. As a developer, you can use Laravel artisan serve to develop and test various functions within the application. It also accepts two additional options. You can use the host for changing application’s address and port. Meanwhile, you can also use the port option to change the port of the application.

You Might Also Like: Install Laravel 5.8 on Server

Laravel 5.7 Custom Artisan Command

Using Laravel, you can easily create custom artisan commands. Just type the following command in the Artisan Console to create a new custom command:

php artisan make:command <command_name>

Now open your terminal and execute the following command.

php artisan make:command CreateEmployeeAccount

Once you execute the above-mentioned command completely, you will get a file in the app/console/Commands directory with the name ‘CreateEmployeeAccount’. Here is the code:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateEmployeeAccount extends Command

{

   /**

    * The name and signature of the console command.

    *

    * @var string

    */

   protected $user = 'command:name';

   /**

    * The console command description.

    *

    * @var string

    */

   protected $description = 'Command description';

   /**

    * Create a new command instance.

    *

    * @return void

    */

   public function __construct()

   {

       parent::__construct();

   }

   /**

    * Execute the console command.

    *

    * @return mixed

    */

   public function handle()

   {

       //

   }

}

Now, it’s time to update the newly created Laravel command. We will define $user to create admin and $description to create user account having admin role. Defining these, we also have to update the role field in the users table.

You Might Also Like: Install Laravel 5.7 on Cloud Server

After this, if you run the php artisan in Laravel, it will show you an error saying the command still not exists. It is because the command is still not registered in the list and is not being read up by the terminal. To register the command, you must update the Kernel.php file located inside the app/Console directory.

protected $commands = [

   Commands\CreateEmployeeAccount::class,

];

Once you update the command in Kernel.php, you can now use the newly created command. You can also check it in the list by using php artisan list command.

Database Migration

The above-mentioned command still doesn’t work for anything, as you haven’t edited any logic in it. So, let’s first create migration tables with the following command:

Now, check your migration folder and you will see the newly created migration table. You can also setup the up() and down() functions for your convenience.

Model

Now, you will have to update you model array with the following code:

protected $fillable = [

   'name', 'email', 'password', 'role'

];

After this, you can update your handle() according to your desired requirements:

$newuser = [

           'name' => 'AYX',

           'email' => 'WYZ@domain.com',

           'password' => bcrypt('secret'),

           'role' => 'Manager'

       ];

Final Words

This brings us to the end of this article. It demonstrates how to create custom composer command using the Artisan command. Using PHP Artisan, you can develop different commands depending upon the needs of your project. If you still have any questions regarding this article, or have any query related to Artisan command, feel free to share them in the comments’ section below.

Share your opinion in the comment section. COMMENT NOW

Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

THERE’S MORE TO READ.