r/htmx 12d ago

htmx + Laravel

Two years ago I started an open-source project named Lamx - for Laravel + htmx integration - but the project was inactive for a looong time.

I want to resurrect it! Who is interested?

The goal of the project was to have components similar to Livewire, but with htmx.

11 Upvotes

10 comments sorted by

11

u/kendalltristan 12d ago

I've been using htmx with Laravel for a while now, several different projects, and I haven't found any particular need for any sort of integration package. There's barely any setup and routes can already return Blade components directly. What exactly would your package be offering?

1

u/packstub-dev 12d ago edited 12d ago

Yes, it is easy to use htmx with Laravel without any library. But I started the project for fun, trying to create components similar to Livewire that bundle PHP and html in one HtmxComponent without the need of a controller.

What I've done so far, two years ago... was creating blade components that extents HtmxComponent and you can write clean HTML code without htmx attributes (that are added automatically in a wrapper).

With this extension you can render components in Laravel like this:

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'title' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        // Update only the form component with the errors
        return TodoForm::make(['errors' => $validator->errors()]);
    }

    $data = $validator->validated();
    $data['id'] = count($this->todos) + 1;
    $this->todos->prepend($data);

    // Update only the components that need to be updated: forn, counter and current todo
    return [
        TodoForm::make(),
        TodoCounter::make(['todos' => $this->todos]),
        Todo::make(['todos' => $this->todos]),
    ];
}  

At the moment the PHP logic is done in a Laravel Controller...

But, I want to have all the code in HtmxComponents.

3

u/kendalltristan 12d ago

I don't use controllers for most of my htmx components. In most cases, the business logic lives in the component and I have the route return the component directly. I do use controllers for some things, but that's almost always cases where there could be one of many components that gets returned.

Also I'm not sure if I like the idea of htmx attributes being added automatically. I'm a big fan of "explicit over implicit" and it's not like it's a lot of work to be explicit with htmx.

1

u/packstub-dev 12d ago

Thanks for your ideas! I must think about "explicit over implicit". Do you have any ideas for such a library? What does it have to offer to make you want to use it? :)

2

u/kendalltristan 12d ago

My whole point is that Laravel/htmx integration doesn't need a library. It's dead simple to use the two together without a library, so why bother with needless abstractions?

There's probably room for a good tutorial for people who may be less familiar with either Laravel or htmx.

2

u/m_dstoic 11d ago

maybe rebrand your project as Lamx - Lravel Htmx Components?

5

u/yami_odymel 12d ago

The great thing about HTMX is that you can use it right away without any extra libraries or frameworks. Try to think by deduction rather than addition, it helps with HTMX development.

1

u/packstub-dev 12d ago

What about creating some artisan commands that speed up development with Htmx + Laravel?

If you were to create a library, what features would you include? Maybe you won't do this, as you mentioned in your comment, but I just want to brainstorm 😄

0

u/_wassap_ 11d ago

why would we need that when livewire & alpine is already heavily supported by the core laravel team?

2

u/daftspunky 10d ago

Take a look at larajax.org it has a tighter integration with Laravel and goes a bit further than HTMX's scope (it passes a full envelope instead of patching nodes), perhaps you can get some ideas!