PASSWORD RESET

Your destination for complete Tech news

Artisan command laravel

2.72K 0
3 min read

Before digging into artisan command and all this. Let’s have a clear idea on Command Line Interface. CLI is text-based UI for file configuration in computer systems in forms of text lines. Operating systems implement a command-line interface in a shell for interactive access to OS functions or services. The two most popular and easy to understand ones are DOS (for Windows) and the bash shell (for Linux and OS X). Each CLI uses its own command syntax, but they all function in a similar way.

Similarly, artisan is CLI for Laravel. It provides helpful commands and extensively used for setting up migrations, routes listings, queueing, class creations, and other related tasks.

If you are not into laravel then you are probably not familiar with artisan commands and if you are then you must be familiar with these following commands.

1) Serve command: The serve command is used to run the application using the PHP development server. This command is generally used for development and testing purposes. 

2) List: To view a list of all available Artisan commands, you may use the list command.

Some of the list commands of laravel 6 that come handy:

  make:channel         Create a new channel class
  make:command         Create a new Artisan command
  make:controller      Create a new controller class
  make:event           Create a new event class
  make:exception       Create a new custom exception class
  make:factory         Create a new model factory
  make:job             Create a new job class
  make:listener        Create a new event listener class
  make:mail            Create a new email class
  make:middleware      Create a new middleware class
  make:migration       Create a new migration file
  make:model           Create a new Eloquent model class
  make:notification    Create a new notification class
  make:observer        Create a new observer class
  make:policy          Create a new policy class
  make:provider        Create a new service provider class
  make:request         Create a new form request class
  make:resource        Create a new resource
  make:rule            Create a new validation rule
  make:seeder          Create a new seeder class
  make:test            Create a new test class

3) help : Every command also includes a “help” screen which displays and describes the command’s available arguments and options. 

Custom command

It’s very easy to create a custom command in laravel. Run the following command.

php artisan make:command <command_name>

Once you execute the above-mentioned command completely, you will get a file in the app/console/Commands directory with the name <command_name>. 

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class Test extends Command{
   /**
      * The name and signature of the console command.
      *
      * @var string
   */
   
   protected $signature = '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() {
      //
   }
}

The handle method will be called when your command is executed. You may place your command logic in this method.

Finally, after following the above steps, you might get surprised why is your command not showing while you do php artisan list. Thats because you havent registered your command yet. To register the command you need to navigate  app/console/Kernel.php then add this line of code Commands\userData::classto the $commands array defination.

Now, we are good to go!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.