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.
composer require takielias/lab
php artisan lab:install
Now npm run dev
Insert @alert
where you want the alert messages to appear in your Blade file. And put your form submit button
as @submit
<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>
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();
}
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());
}
}
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
Please see the changelog for more information on what has changed recently.
composer test
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email taki.elias@email.com instead of using the issue tracker.
MIT. Please see the license file for more information.