Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Using beautiful command prompts in Laravel

Artisan commands in Laravel are truly a blessing. I mean you could create just about any files be it controllers, models, middleware, or provider by knocking a simple command from the CLI.

For instance, if you want to create a model named Post you could just fire the following command like so.

php artisan make:model Post

And a new Post.php model is created in your project under the app/Models directory. Similarly, you can create a controller or any other files using a similar way.

But with the latest release of Laravel, using these artisan commands has become even more delightful. These prompts looks and behaves just like the form inputs you see on the web. They are beautiful and intuitive.

Prompts in native Laravel commands

Essentially, you’ll just need to run the bare-bone command without any arguments and Laravel will prompt you to enter the required arguments beautifully.

For instance, if you run the make:model command without any arguments, Laravel will prompt you to enter the model name in a beautiful way like so.

As you can tell, on running the make:model command, Laravel will prompt you to enter the model name as well as multiple options such as whether you want to create a migration file, a factory file, and so on through beautiful-looking prompts.

Laravel prompts can be used outside Laravel as well. To use them, you’ll need to install the laravel/prompts package in your PHP projects like so.

composer require laravel/prompts

Using prompts in custom commands

All the native Laravel commands are now using these prompts out of the box. But if you want to use these prompts in your custom commands, you can do that as well.

All you need to do is to import the relevant functions from the Laravel\Prompts namespace and use them in your custom commands.

For instance, I have this simple command called artisan app:add-user which adds a new user to the database. Now, if I want to take user input through prompts, I can do that by importing the text function from the Laravel\Prompts namespace and using it under the handle method like so.

use function Laravel\Prompts\text;

public function handle()
{
    $name = text(
        label: 'Name of the user?'
    );
}

If you want to make the field required, you can pass true as the second argument to the text function like so.

$name = text(
    label: 'Name of the user?',
    required: true
);

You can also use the confirm function to take confirmation from the user like so.

use function Laravel\Prompts\confirm;

$isAdmin = confirm(
    label: 'Do you want '. $name .' to be an admin?'
);

Putting it all together, here’s what the handle method of the artisan app:add-user command looks like.

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\text;

public function handle()
{
    $name = text(
        label: 'Name of the user?',
        required: true
    );

    $isAdmin = confirm(
        label: 'Do you want '. $name .' to be an admin?'
    );

    dd('A user named '. $name .' was created.');
}

And here’s it in action.

As you can tell, the artisan app:add-user command is now using beautiful prompts to take user input.

There are other functions available in the Laravel\Prompts namespace which you can use in your custom commands. Prompts to take passwords, multi-select, suggestions, and so on.

You can check out the official documentation to learn more about these prompts.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?