A Beginner’s Guide to the Laravel Artisan Console

Artisan is the command-line tool that Laravel uses to access its applications. It provides a number of commands which can greatly ease your development process.

So if you’re wondering what it can do and how to get started doing it, this is the guide for you. Here’s everything you need to know on how to use the Artisan console in Laravel.

Getting Started

In your terminal, type the command below in the directory of your Laravel application.

$ php artisan list

It will show the list of available commands.

When using commands, you can precede them with the help option to show their available arguments and options. The terminal will display a short description of what they do on the right-hand side.

$ php artisan --help serve

Tip: As with most command-line actions, the syntax is: command [options] [arguments] 

Tinker

Tinker is the REPL (or interactive shell) that Laravel uses. A “read-eval-print loop” (REPL) is a programming environment that takes single user inputs, processes them, and returns a result to the user.

You can compare Tinker to using the Python interpreter, but Tinker is custom-made for Laravel.

This particular environment provides many advantages, like easily running simple tests on the system without constantly writing files.

Note: If you’re using Laravel Sail, add the sail command and your instructions will be executed within the Docker containers of your application.

$ ./sail artisan list

How to Install Tinker

By default, your Laravel app comes with Tinker. If you’ve previously uninstalled it, you can add it again via composer.

$ composer require laravel/tinker

Typing Commands

Tinker allows you to interact with your whole application. To begin using Tinker, type:

$ php artisan tinker

You’re then greeted with the tinker shell.

Related: How to Manipulate Text in PHP With These 9 Functions

Tinker has a command allow list which determines which commands can be run in its shell. To expand this list, you can go to the /vendor/laravel/tinker/config directory which contains tinker.php.

You can edit this file in your preferred text editor to add your command.

'commands' => [
// AppConsoleCommandsExampleCommand::class,
],

Defining Your Own Commands

Apart from the default artisan commands, you can create your own commands. On creation, the commands are automatically stored in the app/Console/Commands directory.

In your new Laravel project, you won’t see the /Command directory. This is normal. The folder is auto-created when you define a new command. See how to generate commands below.

Creating Commands

Define your command’s signature and a description of the class. In the command’s class file, you’ll also define its arguments. Take this example:

$ php artisan make:command GetDailySales

The file below auto-generates on execution. You can place your command logic in the handle method since it’s called when the command executes.


namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class GetDailySales 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
*/
publicfunction__construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
publicfunctionhandle()
{
return0;
}
}

Closure Commands

You can contrast closure commands to have route closures defined instead of using controllers. Similarly, instead of creating command classes (like we did in the “Creating Commands” section), you can just use command closures.

The advantage here is that you’re going to have fewer files to manage and your logic is easier to thumb through.

Read More: High-Level vs. Low-Level Programming Languages, Explained

The Kernel.php file defines console routes to your application. You can define all your closure console commands with the Artisan::command method in this file. The command takes in two arguments: the command signature, and the closure you’re assigning it to.

Artisan::command('report:get {day}', function ($day) {
$this->info("Generating report for: {$day}!");
});

Type-Hinting Dependencies

Type-hinting refers to statically defining the data type a function should return or an argument should take on. Command closures can also type-hint the dependencies you need to get from the service container.

Describing Command Closures

To add a description to a command closure, use the purpose() method.


Artisan::command('text:send {user}', function ($user) {
// ...
})->purpose('Send sms text to a user');

You can view a full list of commands and functionalities on Laravel’s docs.

Learn More Laravel

Laravel has a strong developer community and well-written documentation from its official site. This means that you’ll never run out of places to find help when you get stuck.

Keep practicing with the artisan console, and you’ll realize how powerful and time-saving the commands can be. Laravel isn’t just the Artisan console, though.

Laravel 8 introduces a host of new features to up your web dev game.

Source: makeuseof.com

Related posts

What are AI PCs, and What Makes Them Different?

I Took the Same Photos With My Phone and Camera: Which One Did It Better?

I Really Want My Next Laptop to Have 5G: Here’s Why