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.

0.10 2024-12-21 15:09 UTC

This package is auto-updated.

Last update: 2024-12-21 15:09:47 UTC


README

Latest Version Stars Total Downloads Forks Issues Linkedin

This package provides an easy solution for implementing jQuery AJAX calls and managing responses in Laravel applications.

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

Installation

composer require takielias/lab
php artisan lab:install

Now npm run dev

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.