ymigval/laravel-model-datatable-ssp
Composer 安装命令:
composer require ymigval/laravel-model-datatable-ssp
包简介
Extension designed to seamlessly integrate Laravel models with server-side DataTables. It provides a convenient and efficient way to fetch, transform, and display data from your Laravel models in DataTables.
README 文档
README
Extension designed to seamlessly integrate Laravel models with server-side DataTables. It provides a convenient and efficient way to fetch, transform, and display data from your Laravel models in DataTables.
Installation
To get started, install the package via Composer:
composer require ymigval/laravel-model-datatable-ssp
Usage with Eloquent Models
To use DataTables with an Eloquent model, you can create an instance or query your model and call the datatable() method with column mappings.
use App\Models\Customer; return (new Customer())->datatable([ 'first_name', 'last_name', 'phone' ]);
Alternatively, you can call the static datatable() method:
use App\Models\Customer; return Customer::datatable([ 'first_name', 'last_name', 'phone' ]);
- Replace
Customerwith your Eloquent model. ['first_name' => 'first_name', 'last_name' => 'last_name', 'phone' => 'phone']: Define the mappings of your model's fields.
Customizing Columns
You can customize field values by providing closures in your column mappings.
use App\Models.Customer; return Customer::where('type', 'male')->datatable([ 'first_name', 'last_name', 'active' => function ($field, $row) { return ($field) ? 'Yes' : 'No'; } ]);
- $field: Contains the value of the field in that mapping.
- $row: Contains the values of the fields in the context of the current row.
Adding Additional Columns
You can add additional columns by using closures:
use App\Models\Customer; return Customer::datatable([ 'first_name', 'last_name', function () { return 'additional column #1'; }, function () { return 'additional column #2'; } ]);
Fields in Context
Define model fields in context to access related data or perform custom formatting.
use App\Models\Customer; return Customer::datatable([ 'first_name' => function ($field, $row) { return $field . ' ' . $row->last_name; } ], ['last_name']);
By default, fields added to the context cannot be searched and sorted. You can configure this behavior by adding options to the field:
use App\Models\Customer; return Customer::datatable([ 'first_name' => function ($field, $row) { return $field . ' ' . $row->last_name; } ], ['last_name' => ['orderable' => true, 'searchable' => true]]);
Usage with Query Builder
You can use DataTable with Query Builder by calling dataTable() on a query builder instance.
use Illuminate\Support\Facades\DB; return DB::table('customers') ->datatable([ 'first_name', 'last_name', 'phone' ]);
Transforming Output Data
You can transform the datatable return into various formats such as response, array, or json by specifying it as the third parameter.
By default, a response is returned.
use App\Models\Customer; (new Customer())->datatable( ['first_name', 'last_name', 'phone'], [], 'array' );
use Illuminate\Support\Facades\DB; DB::table('customers')->datatable( ['first_name', 'last_name', 'phone'], [], 'json' );
Advanced Usage
Using Callbacks for Column Mappings
You can use a callback to define columns dynamically.
use App\Models\Customer; return (new Customer())->datatable( function () { return ['first_name', 'last_name', 'phone']; } );
Union Queries
Perform union queries with DataTable.
use App\Models\Customer; return Customer::join('business', 'business.id_customer', '=', 'customers.id') ->datatable( function () { return ['customers.first_name', 'customers.last_name', 'business.name']; } );
You can also add aliases to the fields in the column mapping or fields in context:
use App\Models\Customer; return Customer::join('business', 'business.id_customer', '=', 'customers.id') ->datatable( [ 'customers.first_name AS f_name', 'customers.last_name AS l_name', 'business.name AS aaa', ], ['customers.phone AS contact' => ['orderable' => false, 'searchable' => true]] );
Using Eloquent Relationships
Note on Using Relations
When using relations, there are some limitations:
- Avoid using related fields as column values without a closure.
- To utilize the value of a related field, it should be accessed through a formatting closure. Use the second parameter of the closure to access the value. Remember that the second parameter contains the values of the fields in the context of the current row.
- Related fields cannot be sorted or searched.
Please make sure to add the local key used in the relation to your column mappings or fields in context.
use App\Models\Customer; return Customer::with('business') ->datatable( [ 'first_name', 'last_name', function ($field, $row) { return $row->business->name; }, ], ['id'] // 'id' is the localKey field specified in the relation with 'business' );
For more usage examples, refer to the test cases.
Installing DataTables in Your Application
In the official DataTables documentation: https://datatables.net/, you will find the steps to install the library in your application.
Check out the server-side processing examples: https://datatables.net/examples/server_side/simple.html
Changelog
Please refer to the CHANGELOG for more information about recent changes.
License
The MIT License (MIT). For more information, please see the License File.
ymigval/laravel-model-datatable-ssp 适用场景与选型建议
ymigval/laravel-model-datatable-ssp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 434 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「query」 「model」 「laravel」 「datatables」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ymigval/laravel-model-datatable-ssp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ymigval/laravel-model-datatable-ssp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ymigval/laravel-model-datatable-ssp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
Interfaces for web resource models and services to retrieve and create them
Anax Database Active Record module for model classes.
A PSR-7 compatible library for making CRUD API endpoints
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 434
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-09