PHP code example of imsus / laravel-htmx

1. Go to this page and download the library: Download imsus/laravel-htmx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imsus / laravel-htmx example snippets


use Illuminate\Http\Request;

Route::get('/items', function (Request $request) {
    return view('items', ['items' => Item::all()])
        ->fragmentIf($request->isPartial(), 'rows');
});

$request->isPartial(); // In controllers.
htmx()->isPartial();   // Anywhere via the helper.
Htmx::isPartial();     // Via the facade in services and jobs.

return htmx()->oob(view('todos', [...]), ['todo', 'todo-count']);

Route::post('/items', function (Request $request) {
    $validator = Validator::make($request->all(), ['name' => 'rrors' => $validator->errors()]),
            'form-errors',
            '#form-errors',
        );
    }

    // ...
});

return response($html)->retarget('#rows');

'client' => [
    // ...
    'historyCache' => ['disable' => false],
],

$request->file('avatar'); // Just like any other upload.

// Skip the swap entirely while the poll tag is current.
return htmx()->poll(view('feed', ['items' => $items]), 'news', $current);

// Read the answer from a pre-request prompt.
$reason = $request->prompt();

// Preloads may never be seen — keep them cheap, never write.
if ($request->isPreloaded()) {
    // Serve something light.
}

// Stream HTML updates over one response (hx-sse).
return htmx()->eventStream([
    '<p>warming up</p>',
    ['data' => '<p>done</p>', 'id' => 'e42'],
]);

// Download a file on the side while the response swaps normally.
return response('<span>Started…</span>')->download('/files/report.pdf');
bash
php artisan htmx:install
bash
php artisan vendor:publish --tag="laravel-htmx"
bash
php artisan vendor:publish --tag="laravel-htmx-config"
php artisan vendor:publish --tag="laravel-htmx-views"
php artisan vendor:publish --tag="laravel-htmx-assets"
blade
<div id="form-errors"></div>

@fragment('form-errors')
@foreach ($errors->all() as $error)
<p>{{ $error }}</p>
@endforeach
@endfragment
blade
@php($esm = app(\Imsus\LaravelHtmx\HtmxAssets::class)->variant('htmx.esm.js'))
<script type="module" src="{{ $esm['src'] }}" integrity="{{ $esm['integrity'] }}" crossorigin="anonymous"></script>
bash
php artisan htmx:upgrade-check --path=resources/views --ext=.blade.php