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.

[Webinar] Register Now to Learn Component Based Web Development With Builderius. Click here to register

Creating Custom Composer Commands with PHP Artisan in Laravel

Updated on June 17, 2022

4 Min Read
php artisan

Artisan is the title of the command-line interface that is included with Laravel. It gives a number of accommodating commands for your utilize whereas creating your application. It is driven by the effective Symfony Support component.

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it’ll be automatically made the primary time we run the artisan make:command command.

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.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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' => '[email protected]',

           '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.

How do I run artisan in Laravel?

    • Make a database locally named property utf8_general_ci Download composer https://getcomposer.org/download/ Pull Laravel/php extend from git provider.
    • Rename .env.example record to .envinside your extend root and fill the database data. (Windows won’t let you are doing it, so you have got to open your support cd your extend root registry, and run mv .env.example .env)
    • Open the comfort and cd your extended root directory
    • Run composer introduces or php composer.phar install
    • Run php artisan; key: generate.
    • Run php; artisan migrate.
    • Run php artisan; DB: seed to run seeders, in case any.
    • Run php artisan serve

#####You can now access your project at localhost:8000

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Unleash The Cloud.
Not Budgets.

For 4 Months +
Up To 30 Free Migrations

Cyber Week

SAVINGS
Time Left In Offer
  • 0

    Days

  • 0

    Hours

  • 0

    Minutes

  • 0

    Seconds

40% OFF

On All Plans

GET THE DEAL NOW