The Laravel Ajax Builder is a Laravel package for easily creating AJAX JSON responses. It allows setting status codes, messages, views, data, alerts, and redirect URLs through a fluent interface, streamlining the process of sending AJAX responses in Laravel applications.

Maintainers

Package info

github.com/takielias/laravel-ajax-builder

pkg:composer/takielias/lab

Statistics

Installs: 1 036

Dependents: 1

Suggesters: 0

Stars: 1

Open Issues: 0

1.1.0 2026-05-01 08:25 UTC

README

tests lint Latest Version Total Downloads PHP Version Laravel License Stars Forks Issues Linkedin

Fluent AJAX response builder for Laravel — fetch + FormData on the client, no jQuery.

For an enhanced user experience, it is highly recommended to integrate this package with the Laravel Tablar admin dashboard.

Requirements

Component Minimum
PHP 8.3
Laravel 11.x / 12.x / 13.x
Node 22 LTS (only needed if you bundle the JS helpers)

Installation

composer require takielias/lab
php artisan lab:install

Then build assets with your existing pipeline (npm run build for Vite, or copy the published JS into your bundler entrypoint).

JavaScript usage (no jQuery)

The package ships native window.ajax* helpers that wrap fetch + FormData. The CSRF token is read from <meta name="csrf-token"> automatically.

<form id="ajax-form" action="/products" method="POST">
    @csrf
    <input name="title" required>
    <button type="submit" class="ajax-submit-button">Save</button>
</form>

<script>
    // fired automatically when .ajax-submit-button is clicked, but you
    // can also call the helpers directly:
    document.getElementById('manual-trigger').addEventListener('click', () => {
        const data = new FormData(document.getElementById('ajax-form'));
        window.ajaxPost('/products', data,
            (response) => console.log('saved', response),
            (error)    => console.error('failed', error),
            ()         => console.log('done')
        );
    });
</script>

Usage

Insert @alert where you want the alert messages to appear in your Blade file. And put your form submit button as @submit

Example

<form class="card" action="{{route('product.save')}}" method="post">
    <div class="card-header">
        <h3 class="card-title">Slip form</h3>
    </div>
    <div class="card-body">
        @alert
        
      ...............
      ...............
      
    </div>
    <div class="card-footer text-end">
        @submit
    </div>
</form>

Controller

    function store(SaveProductRequest $saveProductRequest)
    {
        $validated = $saveProductRequest->validated();
        Product::create($validated);
        return Lab::setData(['success' => true])
            ->enableScrollToTop()
            ->setRedirect(route('product.index'))
            ->setSuccess('Product Created Successfully')
            ->setStatus(201)
            ->toJsonResponse();
    }

Request

For request validation

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Takielias\Lab\Facades\Lab;

class SaveProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        return [
            'price' => ['required', 'gt:0'],
            'name' => ['required']
        ];
    }

    protected function failedValidation($validator)
    {
        // Throw the HttpResponseException with the custom response
        throw new HttpResponseException(Lab::setStatus(422)
            ->enableScrollToTop()
            ->setValidationError($validator)->toJsonResponse());
    }
}

Ajax Call

    const productData = {
    product_name: 'Product Name'
    };
    const postUrl = '{{route('
    product.save
    ')}}';
    ajaxPost(postUrl, productData, function (response) {
        console.log(response.data)
    }, function (error) {
    
    }, function (data) {
    
    })

There are also some built in Method ajaxGet, ajaxPost, ajaxPut & ajaxPatch

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email taki.elias@email.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.