Consent

This site uses third party services that need your consent.

Skip to content
Steven Roland
  • Enhancing CLI Experiences with Laravel Prompts

    Laravel Prompts is a powerful package that brings beautiful, user-friendly forms to command-line applications. It offers browser-like features such as placeholder text, real-time validation, and interactive elements, making CLI interactions more intuitive and engaging. In this post, we'll explore Laravel Prompts and demonstrate how to leverage its capabilities in your projects.

    Getting Started

    Laravel Prompts is included by default in recent Laravel releases. For other PHP projects, you can install it via Composer:

    composer require laravel/prompts

    Basic Usage

    Let's start with a simple example using the text function:

    use function Laravel\Prompts\text;
    
    $name = text('What is your name?');

    This will prompt the user with the question and return their input. You can enhance this further with placeholders, default values, and hints:

    $name = text(
        label: 'What is your name?',
        placeholder: 'E.g. Taylor Otwell',
        default: 'Guest',
        hint: 'Please enter your full name'
    );

    Form Validation

    Laravel Prompts shines when it comes to form validation. Here's an example of a password input with validation:

    use function Laravel\Prompts\password;
    
    $password = password(
        label: 'Choose a password',
        validate: fn ($value) => 
            strlen($value) < 8
                ? 'The password must be at least 8 characters.'
                : null
    );

    Multi-step Forms

    For more complex interactions, you can use the form function to create multi-step forms:

    use function Laravel\Prompts\form;
    
    $responses = form()
        ->text('What is your name?', required: true)
        ->password('Choose a password', validate: ['password' => 'min:8'])
        ->confirm('Do you agree to the terms?')
        ->submit();

    This creates a form where users can navigate back to previous questions using CTRL + U.

    Suggested Usages

    • Interactive Installers: Create user-friendly installation processes for your packages or applications.

    • Configuration Wizards: Guide users through complex configuration setups with step-by-step prompts.

    • Database Seeders: Collect data interactively to populate your database during development or testing.

    • Custom Artisan Commands: Enhance your Artisan commands with interactive prompts for a better developer experience.

    • CLI Tools: Build standalone CLI tools with rich user interfaces for tasks like data processing or system management.

    Best Practices

    • Provide Clear Instructions: Use labels and hints to guide users through each input.

    • Implement Validation: Leverage real-time validation to prevent errors and improve user experience.

    • Use Appropriate Input Types: Choose the right prompt type (text, password, select, etc.) for each input.

    • Offer Sensible Defaults: Where possible, provide default values to speed up the process for users.

    • Handle Errors Gracefully: Implement error handling to provide helpful feedback when something goes wrong.

    Laravel Prompts brings a new level of sophistication to command-line interfaces. By leveraging its features, you can create more engaging, user-friendly CLI experiences that rival web interfaces in terms of interactivity and feedback. Whether you're building complex Artisan commands or standalone CLI applications, Laravel Prompts offers the tools to elevate your command-line interactions.

    Remember, while Prompts significantly enhances the CLI experience, it's important to ensure your applications remain functional in environments where Prompts may not be fully supported. Always provide fallback options for critical functionality.

    Happy coding, and may your command-line interfaces be ever more delightful with Laravel Prompts!

    More posts

    Life as a Sonnet: Madeleine L'Engle's Poetic Wisdom

    Madeleine L'Engle's quote from "A Wrinkle in Time" compares life to writing a sonnet, highlighting the balance between structure and creativity. It emphasizes personal responsibility in shaping our lives within given constraints.

    Enhancing User Experience with Laravel Precognition

    Laravel Precognition enables real-time form validation using server-side rules. It integrates with frontend frameworks like Vue.js to provide immediate feedback. Key uses include multi-step forms, dynamic field validation, debounced validation, and file upload checks. Best practices involve maintaining backend validation, handling network errors, and considering performance implications.

    Unexpected Potential: The True Measure of Who We Are

    Jodi Picoult's quote from "My Sister's Keeper" challenges conventional views of identity, suggesting that our true selves are defined by our unexpected capabilities rather than routine actions. It encourages embracing potential and remaining open to self-discovery.