Top 20 Laravel Artisan Commands with Examples for Developers [Updated]

  Top 20 Laravel Artisan Commands for Developers

The Laravel Artisan Commands CLI – is the in-built command-line interface included with Laravel to assist developer while building Laravel applications.

Introduction to Laravel Artisan Commands CLI

Laravel framework comes with three primary tools for command-line interaction:

  • Artisan CLI – a suite of built-in command-line actions with the ability to add more
  • Tinker – Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more.
  • REPL – REPL stands for reading—Eval—Print—Loop, an interactive shell that takes in single user inputs, evaluates them, and returns the result to the user.

In this chapter, we will discuss the Artisan CLI in detail. Artisan provides a rich set of commands for developing your application which is derived from the powerful Symfony Console component.

Why “php artisan..”

All the laravel artisan commands are executed starting with “php artisan …”. If you have noticed, the aretisan is PHP script file located under the root folder of laravel framework installation. So, we are running a php file by passing artisan file to php console. Everything after that is just passed into Artisan as arguments.

 

Learn Laravel Tutorials:

If you are a beginner to Laravel Framework, then we recommend you to read the following topics:

 

Artisan Command Syntax and Usage:

php artisan [options] [arguments]

Get a list of all Artisan commands:

To get a list of all available Artisan commands, you can run php artisan list from the project root folder.

  • php artisan list

The number of commands may vary depending upon the version of Laravel installation.

 

Viewing The Help Screen For A Command

Each command also includes a “help” screen which gives the list of associated options and arguments.
To display the help about any command, just precede the command with help option:


     php artisan help <command-name>
     php artisan help migrate

Here, is a complete list of laravel artisan commands.

Usage:
  command [options] [arguments]

Options:
  -h, --help                           Display this help message
  -q, --quiet                          Do not output any message
  -V, --version                     Display this application version
      --ansi                             Force ANSI output
      --no-ansi                       Disable ANSI output
  -n, --no-interaction          Do not ask any interactive question
      --env[=ENV]                The environment the command should run under.
  -v|vv|vvv, --verbose     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

 

Refer Laravel Artisan Commands official documentation

Must read guides on OpenCart and JavaScript:

Powerful Features of JavaScript That Every Developer Should Know!!

How PHP Connect to MySQL Tutorial with Example

How to install OpenCart on LAMP/WAMP Server?

What is OpenCart? Features of OpenCart!

Ultimate PHP Composer Package Manager Tutorial

Laravel Artisan Commands Cheat Sheet

Let’s have closer look at Top 10 Laravel Artisan Commands for developers while building laravel applications.

Command# Description & Output
php artisan –version Check the current version of laravel installation?

Output:
Laravel Framework version 5.2.45

php artisan down Put Laravel application in “maintenance mode”

Output:
Application is now in maintenance mode.

php artisan env Display the environment laravel is running

Output:
Current application environment: local

php artisan migrate Run Database migrations

This executes all the defined migrations and create database tables.

php artisan serve To start Laravel project. By, default this hosts the application locally at localhost:8000
You can server with different hostname and post using “–host” and “–port” options respectively.
php artisan up Bring UP the laravel application out of maintenance mode
php artisan auth:clear-resets Flush the expired password tokens
php artisan cache:clear Flush the application cache
php artisan cache:table Create a migration for the cache database table
php artisan config:cache Create a cache file for faster configuration loading
php artisan config:clear Remove the configuration cache file
php artisan make:auth Scaffold basic login and registration views and routes
 php artisan make:controller TechCluesBlog  Create a new controller class using artisan command
php artisan make:migration my_blog_post Create a new migration file.

Output:
Created Migration: 2019_01_27_094045_my_blog_post
The new migration file 2019_01_27_094045_my_blog_post.php will be created under ../database/migrations./

php artisan make:model MyBlogPost Create a new Eloquent model class.

Output:
Model created successfully.
The new Model file “MyBlogPost.php” will be created in app/Models or ../app/..

php artisan route:list List all registered routes
php artisan route:clear Remove the route cache file
php artisan route:cache Create a route cache file for faster route registration
php artisan vendor:publish Publish any publishable assets from vendor packages
php artisan view:clear Clear all compiled view files

 

Also, read our complete Laravel Tutorials

How to Programmatically Executing Artisan Commands

The Laravel Artisan commands are designed to be run from the command line, you can also call them from other code. The easiest way is to use Artisan facade. You can either call a command using Artisan::call() (which will return the command’s exit code), or queue a command using Artisan::queue().

Route::get('test-artisan', function () {
$exitCode = Artisan::call('password:reset', [
'userId' => 15, '--sendEmail' => true
]);
});

Writing Custom Artisan Commands:

So far we have covered the Artisan commands that come in-built with Laravel. But, you can also write your own commands. Laravel allows developers to create custom artisan commands to use in web applications.

Wow! There is also an Artisan command to create a new custom artisan command. Create a new Artisan command

php artisan make:console YourCommandName

This will generate a new Artisan command in app/Console/Commands /{YourCommandName}.php. The first argument should be the class name of the command, and you can optionally pass a --command parameter to define what the terminal command will be (e.g., appname:action).

Example to create artisan command:
php artisan make:console WelcomeNewUsers –command=email:newusers

This will create the following class file:

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class WelcomeNewUsers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'email:newusers';
/**
* 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()
{
//
}
}

This auto-generated class file contains the signature and description of the user-defined command. When the user executes the command using artisan CLI, the handle() function is automatically invoked to execute the functionalities.

Registering Artisan Commands:

After creating the custom artisan command, we have to register them with Kernel to get them executed successfully. To register a newly created artisan command, open app/Console/Kernel.php and you’ll see an array of command class names under the $commands property. To register your new command, add its class to this array.

class EmailNewUsers extends Command
{
public function handle()
{
User::signedUpThisWeek()->each(function ($user) {
Mail::send(
'emails.welcome',
['name' => $user->name],
function ($m) use ($user) {
$m->to($user->email)->subject('Welcome!');
}
);
});
}

Now every time you run php artisan email:newusers, this command will grab
every user that signed up this week and send them the welcome email.

Conclusion

Now, we have learned the basics of Laravel Artisan Commands CLI, the rich set of artisan commands, writing custom commands, and programmatically calling artisan commands. The top 10 Laravel artisan commands mentioned above are must for every laravel developer. We hope you have learned something from this post.

Leave a Reply

Your email address will not be published. Required fields are marked *