Get your next remote job on LaraJobs.
1 contributor Edit on GitHub Laravel Packages

Laravel Prompts: build delightful Artisan commands

Table of contents:

Laravel Prompts: build delightful Artisan commands

Introduction

Laravel Prompts was introduced by Jess Archer at Laracon US on July 19, 2023.

The package is widely used in Laravel to drastically improve the developer experience and you can also take advantage of it.

This is a tutorial to help you get started with it.

Requirements for Laravel Prompts

Laravel Prompts only supports macOS, Linux, and Windows with WSL. Outside of WSL, it will fall back onto Symfony’s implementation of the various features Laravel Prompts offers.

You will also need at least Laravel 10 and PHP 8.1.

How to install Laravel Prompts

Laravel Prompts only requires the package to be installed. There’s no configuration file or service provider to publish.

composer require laravel/prompts

Now, we can start building awesome prompts for your Artisan commands.

Learn the basics by building

If you ever built your own Artisan commands, you will appreciate how easy to use Laravel Prompts is.

Let’s create a new command for the sake of this tutorial:

php artisan make:command MakePokemonCommand

Now, instead of the following code:

namespace App\Console\Commands;
 
use App\Models\User;
use Illuminate\Console\Command;
 
class MakePokemonCommand extends Command
{
protected $signature = 'make:pokemon';
 
protected $description = 'Create a new Pokémon';
 
public function handle()
{
$name = $this->ask('What should the name be?');
 
}
}

You can use the functions Laravel Prompts provides and enjoy an improved output:

namespace App\Console\Commands;
 
use App\Models\User;
use Illuminate\Console\Command;
use function Laravel\Prompts\text;
 
class MakePokemonCommand extends Command
{
protected $signature = 'make:pokemon';
 
protected $description = 'Create a new Pokémon';
 
public function handle()
{
$name = text('What should the name be?');
 
Pokemon::create(compact('name'));
 
$this->info("$name was successfully created!");
}
}

Easy, right?

Here’s a screenshot of the command in action.

Our make:pokemon command in action thanks to Laravel Prompts

Add a multi-select

Let’s go even further and add type selection:

$types = multiselect(
label: 'What types should your Pokémon have?',
options: [
'Bug', 'Dark', 'Dragon', 'Electric', 'Fairy', 'Fighting', 'Fire', 'Flying', 'Ghost', 'Grass', 'Ground', 'Ice', 'Normal', 'Poison', 'Psychic', 'Rock', 'Steel', 'Water',
],
required: true,
scroll: 10,
validate: function (array $values) {
return ! in_array(count($values), [1, 2])
? 'A maximum of two types can be assigned to a Pokémon.'
: null;
}
);
  • Have the 18 possible types as options.
  • The prompt is required to have an answer.
  • We display 10 choices at once.
  • We ensure that 1 or 2 types only can be assigned.
  • We leverage PHP’s named arguments to keep our code informative and omit some arguments.

Here’s how the refreshed Artisan command using Laravel Prompts looks:

make:pokemon with multiselect and validation.

Laravel Prompts is so easy to use!

Add a spinner (loading animation)

Laravel Prompts lets you use a beautiful loading animation, effortlessly. Just as the rest of the API, it’s as simple as calling the spin() function:

use function Laravel\Prompts\spin;
 
$response = spin(
fn () => Http::get('http://example.com'),
'Fetching response...'
);

According to the documentation, the spin function requires the pcntl PHP extension to trigger the animation. When it’s not available, a static version of the spinner will appear instead.

Now that you got the gist of Laravel Prompts, you can refer to the super easy to understand documentation and continue your journey alone.

Contribute to Laravel Prompts

If you ever encounter a bug or feel like Laravel Prompts needs one more feature, you might want to send your pull requests directly to the official repository: https://github.com/laravel/prompts

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

Laravel 10 is out! Here are every new features and changes. Laravel 10 is out! Here are every new features and changes.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new features and changes.

Modified on Nov 2, 2023

A complete history of Laravel's versions (2011-2023) A complete history of Laravel's versions (2011-2023)

What's the current version of Laravel? Is there any LTS release you can rely on? And what about the history of the framework? Let's find out!

Modified on Oct 15, 2023

Laravel 10: the upgrade guide from version 9 Laravel 10: the upgrade guide from version 9

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

Modified on Nov 2, 2023

Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -