mrkacmaz/laravel-ssp

处理Eloquent模型的服务器端处理(SSP),支持动态过滤、排序和分页。

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

This package is auto-updated.

Last update: 2024-09-23 15:05:20 UTC


README

SSP-INIT是一个Laravel包,旨在简化Eloquent模型的服务器端处理(SSP),支持动态过滤、排序和分页。该包通过抽象复杂的后端操作,简化了数据密集型应用程序的开发。

特性

  • 动态过滤:根据请求参数应用过滤器。
  • 排序:根据任何模型属性对数据进行排序。
  • 分页:根据客户端请求高效地分页数据。
  • 预加载:支持预加载关系以优化查询性能。
  • 响应编码:可选地编码响应数据。

要求

  • PHP ^8.2
  • Laravel ^8.0

安装

要安装SSP-INIT包,请在Laravel项目根目录下运行以下命令

composer require mrkacmaz/laravel-ssp

用法

以下是如何在Laravel控制器中使用SSP特质的示例

控制器

<?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文件

<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文件

$(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'},
        ],
    });
});

在这个例子中,UserController使用SSP特质根据请求中指定的参数处理User模型的数据。

配置

开始使用SSP-INIT无需额外配置,因为它使用Laravel的默认配置。

支持

如果您遇到问题,请通过在GitHub上提交问题来告知我们。

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件。