Home 9 Software Development 9 Understanding Artisan: Powerful Command-Line Interface in Laravel 

Understanding Artisan: Powerful Command-Line Interface in Laravel 

Artisan is Laravel's command-line interface that pursues a clear goal: to make the development process more efficient. Let's explore this feature and look at essential Artisan commands every Laravel developer should know about.
Daniel Zacharias

Ygor Ribeiro

June 1, 2023
laravel

In today’s day and age, you know that building an app requires more than just coding knowledge ― you must also be innovative and efficient.

That’s precisely why frameworks like Laravel exist. Laravel’s primary purpose is to simplify the development of PHP applications, allowing engineers to create innovative solutions with higher productivity and lower costs.

As such, Laravel provides numerous functionalities, including Artisan ― a Command-Line Interface (CLI) which enables developers to interact with the framework, perform various tasks, and streamline their workflow through a text-driven interface. 

Therefore, understanding this CLI and its commands is a must for every success-striving Laravel developer.

What is Artisan?

Artisan is Laravel’s CLI. It provides developers with commands that help them generate components, run tests, migrate databases, and more.

One of the most valuable aspects of Artisan is its capability to produce standard code for routine tasks. This can save you significant time as a developer since you don’t have to rewrite the same code whenever needed. 

Simply put, Artisan streamlines the development process and enhances efficiency by automating common tasks.

8 essential Artisan commands

To use Artisan and its commands, open a terminal window and go to the root directory. Then, enter “php artisan” and add the appropriate command.

Here’s the list of useful Artisan commands you should know about:

php artisan list

Let’s start with the one that allows you to view the list of all available Artisan commands. It displays a table with names and descriptions of all commands.  

php artisan help

Another useful command that allows you to see commands’ arguments and options. 

To view a help screen, type “help” before the specific command’s name. For example: “php artisan help migrate”.

php artisan make:model

This command creates a new model file in the app/Models directory. The class will have a standard structure and functions.

php artisan make:controller

Use “php artisan make:controller” to generate a new controller class in the app/Http/Controllers directory. For instance, “php artisan make controller UserController”.

With Artisan, you can also create a controller and a model together by typing “php artisan make:controller UserController -m User”.

php artisan make:migration

This one creates a new file in the database/migrations directory. (Migrations allow you to adjust your database schema over time in a structured way.)

php artisan migrate

The command executes all pending migrations, which in turn updates the database schema to match the current status of the application. 

php artisan test 

It runs all the test cases in the tests/Unit directory, significantly streamlining the testing process by allowing you to run all the tests with a single command.

php artisan db:seed

Use this command to seed the database with test data. This allows you to test your app without manually generating test data each time.

How to create custom commands in Artisan’s command-line interface?

Artisan’s magic goes beyond built-in commands ― it also allows you to create your own!

To create a new custom command, enter the following command into the Artisan Console:

php artisan make:command <command_name>

Let’s look at an example.

First, open your terminal and run: “php artisan make:command CreateProfile”. You’ll get a file “CreateProfile” in the app/console/Commands directory. 

Here’s the code:

1. <?php

2.

3. namespace App\Console\Commands;

4.

5. use Illuminate\Console\Command;

6.

7. class CreateProfile extends Command

8.

9. {

10.

11. /**

12.

13. * The name and signature of the console command.

14.

15. *

16.

17. * @var string

18.

19. */

20.

21. protected $user = ‘command:name’;

22.

23. /**

24.

25. * The console command description.

26.

27. *

28.

29. * @var string

30.

31. */

32.

33. protected $description = ‘Command description’;

34.

35. /**

36.

37. * Create a new command instance.

38.

39. *

40.

41. * @return void

42.

43. */

44.

45. public function __construct()

46.

47. {

48.

49. parent::__construct();

50.

51. }

52.

53. /**

54.

55. * Execute the console command.

56.

57. *

58.

59. * @return mixed

60.

61. */

62.

63. public function handle()

64.

65. {

66.

67. //

68.

69. }

70.

71. }

The next step is to update the Laravel command you just created. You’ll need to define $user (to create admin) and $description (to make the user account with admin role).

After this, you must also register your new command by updating the Kernel.php file inside the app/Console directory.

1. protected $commands = [

2.

3. Commands\CreateProfile::class,

4.

5. ];

Aaaand, your brand-new command is ready for use! You may now see it by executing the “php artisan list” command and, of course, employ it in your project.

Maximizing efficiency with Laravel’s Artisan command-line interface

Using Artisan is a fantastic way for you or your engineering talent to build modern apps quickly and efficiently. The CLI enables developers to focus on creating great solutions without wasting their energy on time-consuming, repetitive tasks. 

However, to get the most out of Artisan and truly maximize efficiency, it’s crucial to master its commands (I hope this article helps!). Developers should also leverage Artisan’s customizability to explore options and make custom commands that fit specific development needs. 

Overall, Laravel’s Artisan command-line interface is a handy tool that can greatly benefit the development process and contribute to the success of your projects!

Get the best of Code Power News in your inbox every week

    You may also like

    Principles and Benefits of Reactive Programming

    Principles and Benefits of Reactive Programming

    Unlike traditional programming, reactive programming revolves around asynchronous data streams. However, code is usually written linearly, one step after another. Reactive programming offers a way for developers to deal with scenarios where events occur unpredictably...

    Get the best of Code Power News in your inbox every week