PHPackages                             mauricius/laravel-htmx - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mauricius/laravel-htmx

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mauricius/laravel-htmx
======================

Laravel helper library for Htmx

v0.10.0(3mo ago)363112.7k↓27.9%17[2 issues](https://github.com/mauricius/laravel-htmx/issues)1MITPHPPHP ^8.2CI passing

Since Nov 2Pushed 3mo ago11 watchersCompare

[ Source](https://github.com/mauricius/laravel-htmx)[ Packagist](https://packagist.org/packages/mauricius/laravel-htmx)[ Docs](https://github.com/mauricius/laravel-htmx)[ RSS](/packages/mauricius-laravel-htmx/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (10)Versions (16)Used By (1)

laravel-htmx
============

[](#laravel-htmx)

Laravel integration for [htmx](https://htmx.org/).

[![Latest Version on Packagist](https://camo.githubusercontent.com/935d59a4bbc53c92f416fac5e8f9dda308e974eb3dcbc67d98d9af5ac1f8bff2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61757269636975732f6c61726176656c2d68746d782e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mauricius/laravel-htmx)[![GitHub Tests Action Status](https://camo.githubusercontent.com/750dddd8c41264890c7d0b609b5d5db4e0d318f0aeadb331f7d6eaa20d5f0e0c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61757269636975732f6c61726176656c2d68746d782f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473)](https://github.com/mauricius/laravel-htmx/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/31d9120728c0e11e13d702887faab1bed7f0127c5f1db9a35836c7e60ec08f2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61757269636975732f6c61726176656c2d68746d782e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mauricius/laravel-htmx)

Supported Laravel Versions &gt;= v8.80.0.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mauricius/laravel-htmx
```

To install htmx please browse [their documentation](https://htmx.org/docs/#installing)

Usage
-----

[](#usage)

### Request

[](#request)

You can resolve an instance of the `HtmxRequest` from the container which provides shortcuts for reading the htmx-specific [request headers](https://htmx.org/reference/#request_headers).

```
use Mauricius\LaravelHtmx\Http\HtmxRequest;

Route::get('/', function (HtmxRequest $request)
{
    // always true if the request is performed by Htmx
    $request->isHtmxRequest();
    // indicates that the request is via an element using hx-boost
    $request->isBoosted();
    // the current URL of the browser
    $request->getCurrentUrl();
    // true if the request is for history restoration after a miss in the local history cache
    $request->isHistoryRestoreRequest()
    // the user response to an hx-prompt
    $request->getPromptResponse();
    // 	the id of the target element if it exists
    $request->getTarget();
    // the name of the triggered element if it exists
    $request->getTriggerName();
    // the id of the triggered element if it exists
    $request->getTriggerId();
});
```

### Response

[](#response)

- `HtmxResponseClientRedirect`

htmx can trigger a client side redirect when it receives a response with the `HX-Redirect` [header](https://htmx.org/reference/#response_headers). The `HtmxResponseClientRedirect` makes it easy to trigger such redirects.

```
use Mauricius\LaravelHtmx\Http\HtmxResponseClientRedirect;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseClientRedirect('/somewhere-else');
});
```

- `HtmxResponseClientRefresh`

htmx will trigger a page reload when it receives a response with the `HX-Refresh` [header](https://htmx.org/reference/#response_headers). `HtmxResponseClientRefresh` is a custom response class that allows you to send such a response. It takes no arguments, since htmx ignores any content.

```
use Mauricius\LaravelHtmx\Http\HtmxResponseClientRefresh;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseClientRefresh();
});
```

- `HtmxResponseStopPolling`

When using a [polling trigger](https://htmx.org/docs/#polling), htmx will stop polling when it encounters a response with the special HTTP status code 286. `HtmxResponseStopPolling` is a custom response class with that status code.

```
use Mauricius\LaravelHtmx\Http\HtmxResponseStopPolling;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseStopPolling();
});
```

For all the remaining [available headers](https://htmx.org/reference/#response_headers) you can use the `HtmxResponse` class.

```
use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse())
        ->location($location) // Allows you to do a client-side redirect that does not do a full page reload (also supports arrays)
        ->pushUrl($url) // pushes a new url into the history stack
        ->replaceUrl($url) // replaces the current URL in the location bar
        ->reswap($option) // Allows you to specify how the response will be swapped
        ->retarget($selector); // A CSS selector that updates the target of the content update to a different element on the page
});
```

Additionally, you can trigger [client-side events](https://htmx.org/headers/hx-trigger/) using the `addTrigger` methods.

```
use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse())
        ->addTrigger("myEvent")
        ->addTriggerAfterSettle("myEventAfterSettle")
        ->addTriggerAfterSwap("myEventAfterSwap");
});
```

If you want to pass details along with the event you can use the second argument to send a body. It supports strings or arrays.

```
use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse()
        ->addTrigger("showMessage", "Here Is A Message")
        ->addTriggerAfterSettle("showAnotherMessage", [
            "level" => "info",
            "message" => "Here Is A Message"
        ]);
});
```

You can call those methods multiple times if you want to trigger multiple events.

```
use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse())
        ->addTrigger("event1", "A Message")
        ->addTrigger("event2", "Another message");
});
```

### Render Blade Fragments

[](#render-blade-fragments)

This library also provides a basic Blade extension to render [template fragments](https://htmx.org/essays/template-fragments/).

The library provides two new Blade directives: `@fragment` and `@endfragment`. You can use these directives to specify a block of content within a template and render just that bit of content. For instance:

```
{{-- /contacts/detail.blade.php  --}}

            @fragment("archive-ui")
                @if($contact->archived)
                    Unarchive
                @else
                    Archive
                @endif
            @endfragment

        Contact
        {{ $contact->email }}

```

With this fragment defined in our template, we can now render either the entire template:

```
Route::get('/', function ($id) {
    $contact = Contact::find($id);

    return View::make('contacts.detail', compact('contact'));
});
```

Or we can render only the `archive-ui` fragment of the template by using the `renderFragment` macro defined in the `\Illuminate\View\View` class:

```
Route::patch('/contacts/{id}/unarchive', function ($id) {
    $contact = Contact::find($id);

    // The following approaches are equivalent

    // Using the View Facade
    return \Illuminate\Support\Facades\View::renderFragment('contacts.detail', 'archive-ui', compact('contact'));

    // Using the view() helper
    return view()->renderFragment('contacts.detail', 'archive-ui', compact('contact'));

    // Using the HtmxResponse Facade
    return \Mauricius\LaravelHtmx\Facades\HtmxResponse::renderFragment('contacts.detail', 'archive-ui', compact('contact'));

    // Using the HtmxResponse class
    return (new \Mauricius\LaravelHtmx\Http\HtmxResponse())
        ->renderFragment('contacts.detail', 'archive-ui', compact('contact'));
});
```

#### OOB Swap support

[](#oob-swap-support)

htmx supports updating multiple targets by returning multiple partial responses with [`hx-swap-oob`](https://htmx.org/docs/#oob_swaps). With this library you can return multiple fragments by using the `HtmxResponse` as a return type.

For instance, let's say that we want to mark a todo as completed using a PATCH request to `/todos/{id}`. With the same request, we also want to update in the footer how many todos are left:

```
{{-- /todos.blade.php  --}}

                    @fragment("todo")
                         $todo->done])>
                            done)
                                hx-target="#todo-{{ $todo->id }}"
                                hx-swap="outerHTML"
                            />
                            {{ $todo->name }}

                    @endfragment

                @fragment("todo-count")

                        {{ $left }} items left

                @endfragment

```

We can use the `HtmxResponse` to return multiple fragments:

```
Route::patch('/todos/{id}', function ($id) {
    $todo = Todo::find($id);
    $todo->done = !$todo->done;
    $todo->save();

    $left = Todo::where('done', 0)->count();

    return HtmxResponse::addFragment('todomvc', 'todo', compact('todo'))
        ->addFragment('todomvc', 'todo-count', compact('left'));
});
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [mauricius](https://github.com/mauricius)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance80

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~102 days

Recently: every ~113 days

Total

13

Last Release

106d ago

PHP version history (4 changes)v0.1.0PHP ^8.0

v0.2.0PHP ^8.0|^8.1|^8.2

v0.6.0PHP ^8.1

v0.10.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e3aca6f811144bbbc5d4408f2b632ac387df07dc02c1981484494694ed793502?d=identicon)[mauricius](/maintainers/mauricius)

---

Top Contributors

[![mauricius](https://avatars.githubusercontent.com/u/7000852?v=4)](https://github.com/mauricius "mauricius (50 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![asbiin](https://avatars.githubusercontent.com/u/25419741?v=4)](https://github.com/asbiin "asbiin (3 commits)")[![parijke](https://avatars.githubusercontent.com/u/7671998?v=4)](https://github.com/parijke "parijke (1 commits)")[![robertogallea](https://avatars.githubusercontent.com/u/19411470?v=4)](https://github.com/robertogallea "robertogallea (1 commits)")

---

Tags

laravelhtmxlaravel-htmx

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mauricius-laravel-htmx/health.svg)

```
[![Health](https://phpackages.com/badges/mauricius-laravel-htmx/health.svg)](https://phpackages.com/packages/mauricius-laravel-htmx)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
