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.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

Creating Custom Composer Commands with PHP Artisan in Laravel

Updated on February 17, 2023

5 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. Experience its capabilities magnified with optimized Laravel hosting solutions.

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.

Host PHP Websites with Ease [Starts at $11 Credit]

  • Free Staging
  • Free backup
  • PHP 8.0
  • Unlimited Websites

TRY NOW

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.

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'

       ];

Experience Seamless Laravel Hosting with Cloudways

Managed Laravel Hosting Tailored to Perfection – Cloudways Unlocks a World of Possibilities for Your Projects.

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.

Q) What is the purpose of Artisan commands in Laravel?

A) Artisan commands in Laravel serve as a command-line interface (CLI) for automating tasks, managing migrations, generating code scaffolding, running tests, and facilitating various other development and management tasks in Laravel applications.

Q) Can I override or extend existing Artisan commands in Laravel?
A) Yes, you can override or extend existing Artisan commands in Laravel by creating a custom command that extends the base command class and implementing your desired logic or functionality.

Q) Are there any resources or tutorials available for learning more about custom Artisan commands in Laravel?

A) Yes, you can find the official Laravel documentation and resources like Laracasts and online articles from the Laravel community to learn about custom Artisan commands in Laravel. These provide tutorials, examples, and comprehensive guidance for creating custom Artisan commands.

Q) Can I use custom Artisan commands to automate common tasks in my Laravel project?
A) Yes, you can use custom Artisan commands in Laravel to automate common tasks, such as running migrations, seeding the database, generating code, performing scheduled tasks, and executing any custom logic, providing a convenient and efficient way to automate repetitive actions in your Laravel project.

Share your opinion in the comment section. COMMENT NOW

Share This Article

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!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now