mrkacmaz/laravel-ssp

Process server-side processing (SSP) for an Eloquent model, supporting dynamic filtering, sorting, and pagination.

v0.1-beta 2024-04-22 10:08 UTC

This package is auto-updated.

Last update: 2024-04-23 14:11:11 UTC


README

SSP-INIT is a Laravel package designed to facilitate server-side processing (SSP) for Eloquent models, supporting dynamic filtering, sorting, and pagination. This package simplifies the development of data-intensive applications by abstracting complex server-side operations.

Features

  • Dynamic Filtering: Apply filters based on request parameters.
  • Sorting: Sort data by any model attribute.
  • Pagination: Efficiently paginate the data based on the client's request.
  • Eager Loading: Supports eager loading of relationships to optimize query performance.
  • Response Encoding: Optionally encode the response data.

Requirements

  • PHP ^8.2
  • Laravel ^8.0

Installation

To install the SSP-INIT package, run the following command in the root of your Laravel project:

composer require mrkacmaz/laravel-ssp

Usage

Here is a basic example of how to use the SSP trait in a Laravel controller:

Controller

<?php

namespace App\Http\Controllers;

use Mrkacmaz\LaravelSsp\Traits\SSP;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    use SSP;

    public function index(Request $request)
    {
        if ($request->expectsJson()){
            $data = self::processSSP($request, User::class);
            return response()->json($data);
        }
        return view('welcome');
    }
}

Blade File

<x-app-layout>
    <table class="table table-hover">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Full Name</th>
            <th scope="col">Email</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</x-app-layout>

JS File

$(function (){
    $('.table').DataTable({
        serverSide: true,
        processing: true,
        ajax: {
            url: window.location,
            type: 'GET',
            dataSrc: function (data) {
                return JSON.parse(atob(data.data));
            }
        },
        columns: [
            {data: 'id'},
            {data: 'name'},
            {data: 'email'},
        ],
    });
});

In this example, the UserController utilizes the SSP trait to process data for the User model according to the parameters specified in the request.

Configuration

No additional configuration is required to start using SSP-INIT, as it uses Laravel's default configurations.

Support

If you are having issues, please let us know by submitting an issue on GitHub.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.