How to add a custom Livewire form to Backpack

One of the biggest headaches in web development is managing frontend and backend interactions. I’m talking about smooth validation resp...

Karan Datwani
Karan Datwani
Share:

One of the biggest headaches in web development is managing frontend and backend interactions. I’m talking about smooth validation responses, authentication, state management, and all those tricky areas. And... if you’re a Laravel developer and haven’t checked out Livewire yet, you’re missing out on something special!

In this article, I'll talk about Livewire, showcase its benefits with an example Form and show how to use it within the Backpack Admin Panel.

What is Livewire?

Livewire is the secret sauce that simplifies state management by automatically syncing your UI. It makes your Laravel apps more interactive without writing tons of JavaScript. That means less code maintenance and fewer bugs! Plus, it works with Blade templates, so you’re in familiar territory. Livewire is one of the most popular Laravel packages, topping the charts on GitHub for good reason.

Personally, Livewire lets me focus more on business logic rather than getting tangled in the complexities of JavaScript and APIs. If simplicity and efficiency are your goals, Livewire is the way to go.

Setting up Livewire

Setting up Livewire is quick and easy. Follow these steps:

  1. Install Livewire: Run this in your terminal:

    composer require livewire/livewire
    
  2. Include Livewire Scripts: Add these to your main Blade layout (usually resources/views/layouts/app.blade.php):

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <!-- Other head stuff -->
        @livewireStyles
    </head>
    <body>
        <!-- Your awesome content -->
        @livewireScripts
    </body>
    </html>
    

    Note: If you're using Livewire v3, these assets are automatically injected, so this step is optional.

Your Livewire setup is ready to work its magic and make your life easier.

Building a Livewire Form

Now, let’s create a simple Livewire form to see how easy it is to build dynamic forms without writing JavaScript.

Step 1: Create a Livewire Component

Let's create a Livewire component called ContactForm:

php artisan make:livewire ContactForm

This will create two files:

  • A Blade view file at resources/views/livewire/contact-form.blade.php
  • A component class at app/Livewire/ContactForm.php

Step 2: Define the Component Class

In the ContactForm.php file, we add the logic:

namespace App\Livewire;

use Livewire\Component;

class ContactForm extends Component
{
    public $name, $email, $message;

    protected $rules = [
        'name' => 'required|min:3',
        'email' => 'required|email',
        'message' => 'required|min:10',
    ];

    public function submit()
    {
        $this->validate();
        // Handle form submission, like sending an email or saving to the database
        session()->flash('message', 'Your message has been sent!');
    }

    public function render()
    {
        return view('livewire.contact-form');
    }
}

Step 3: Define the Blade View

In contact-form.blade.php, add the following code:

<div class="container mt-5">
    @if (session()->has('message'))
        <div class="alert alert-success">{{ session('message') }}</div>
    @endif

    <form wire:submit.prevent="submit">
        <div class="mb-3">
            <label for="name" class="form-label">Name</label>
            <input type="text" id="name" class="form-control @error('name') is-invalid @enderror" wire:model="name">
            @error('name') 
                <div class="invalid-feedback">{{ $message }}</div> 
            @enderror
        </div>

        <div class="mb-3">
            <label for="email" class="form-label">Email</label>
            <input type="email" id="email" class="form-control @error('email') is-invalid @enderror" wire:model="email">
            @error('email') 
                <div class="invalid-feedback">{{ $message }}</div> 
            @enderror
        </div>

        <div class="mb-3">
            <label for="message" class="form-label">Message</label>
            <textarea id="message" rows="4" class="form-control @error('message') is-invalid @enderror" wire:model="message"></textarea>
            @error('message') 
                <div class="invalid-feedback">{{ $message }}</div> 
            @enderror
        </div>

        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>

Here! pay attention to:

  • wire:model: This directive binds your input fields directly to the component's public properties (name, email, message). It enables two-way data binding, meaning any changes to the input fields automatically update the corresponding properties in your Livewire component—no need for JavaScript!
  • wire:submit.prevent: This prevents the default form submission and calls the submit method in your Livewire component instead, handling everything behind the scenes.
  • Session & error messages: Using @error('field') & session(), you can instantly display success or validation messages after submitting the form without page reloads.

Step 4: Display the Form

Final step, Include the component in your page view (ex home.blade.php). For now, Let's display it to a Backpack's custom page:

  1. Create a custom page for your Backpack admin panel:
php artisan backpack:page ContactPage
  1. Add it to the created blade view:
{{-- contact_page.blade.php --}}
-Go to <code>{{ $page }}</code> to edit this view or <code>{{ $controller }}</code> to edit the controller.
+@livewire('contact-form')

image

Now, when you visit your form page, you’ll see how Livewire handles form validation, two-way data binding, and state management—all without needing any custom JavaScript.

Conclusion

So, to wrap it up, Livewire is a total time saver for Laravel developers! It simplifies handling frontend and backend stuff, syncing your UI without needing much JavaScript. That means less hassle with code and fewer bugs.

No extra effort is needed to make Livewire components work within Backpack — just include them, and they’ll work naturally. With Livewire, you get more time to focus on your business logic instead of getting lost in tech details. Definitely give it a try — you won’t regret it!

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

I'll be creating a POS for my admin using this. It is going to be easy & fun 😄👨‍💻
This is really crazy. It would be great if team can apply some demo in "backpack demo". Waiting..
Cheers for this @Karan, I'm currently exploring Livewire for my BackPack setup, mainly for supercharging dynamic modals but noticing other areas where it could prove useful for my frontend users!
Would you still be able to use the Backpack variables in the Custom Page you created with Backpack?

Latest Articles

Wondering what our community has been up to?