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.

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.

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.

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.

Typing Commands

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

You’re then greeted with the tinker shell.

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.

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:

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.

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.

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.

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.