dykhuizen/laravel-datatable
Composer 安装命令:
composer require dykhuizen/laravel-datatable
包简介
A Laravel package that contains queries for constructing a datatable
README 文档
README
Package for converting a request into a datatable response with searching, sorting, filtering, pagination, and data minimization in Laravel 7+.
Setup
Composer
Pull this package in through Composer
composer require dykhuizen/laravel-datatable
Usage
Use Datatable trait inside your Eloquent model(s). This single trait gives you access the following traits:
- Sortable - Applies a list of order by queries for specified columns
- Searchable - Applies a search query for selected columns
- Filterable - Applies a list of filters to the query based on columns selected
- Paginateable - Either runs the
$eloquent->get()or$eloquent->paginate()methods based on the request - SimplePaginateable - Either runs the
$eloquent->get()or$eloquent->simplePaginate()methods based on the request - Selectable - Selects the response data to be returned
use Dykhuizen\Datatable; use Illuminate\Database\Eloquent\Model; use Dykhuizen\Datatable\Datatable; class User extends Model { use Datatable; }
GET http://localhost:8080/api/users sortColumns=name,id& sortOrder=asc,desc& searchColumns=name& search=testingName& page=1& per_page=5& filter=deleted_at& filter_deleted_at=false& selectableFields=id,name,role.name
Sortable
The sortable columns and order default to the keys sortColumns and sortOrder respectively.
These keys can be overwritten on a per model instance by setting the $sortableColumnsKey and $sortableOrderKey variables.
The values are expected to be comma delimited values and work with HasOne and BelongsTo relations
Ex:
sortColumns = 'id,username,role.id'
sortOrder = 'asc,desc,desc'
If you wish to have a field ordered which does not exist within a model's fields, you can optionally define a [property]Sortable function
Below is an address example
public function addressSortable($query, $direction) { return $query->join('profiles', 'users.id', '=', 'profiles.user_id')->orderBy('address', $direction)->select('users.*'); }
Searchable
The searchable columns and search default to the keys searchColumns and search respectively.
These keys can be overwritten on a per model instance by setting the $searchableColumnsKey and $searchableSearchKey variables.
The values are expected to be comma delimited values and work with any type of relation
Ex:
searchColumns = 'username,profile.address'
search = 'phrase to search on'
If you wish to have a field searched which does not exist within a model's fields, you can optionally define a [property]Searchable function
Below is an address example
public function addressSearchable($query, $search) { return $query->whereHas('profile', function($query) use ($search) { return $query->where('address', '=', $search); }); }
Filterable
The filterable column defaults to the key filter
This key can be overwritten on a per model instance by setting the $filterableFieldsKey variable.
The values are expected to be comma delimited values and work with any type of relation
Additionally, the filter values are expected to be passed as unique fields.
For example, filter on status expends the keys filter = status and filter_status = 'yes,no,maybe to exist
Ex:
filter = 'deleted_at,profile.deleted_at'
filter_deleted_at = 'false'
filter_profile.deleted_at = 'yes'
If you wish to have a field filtered which does not exist within a model's fields or has a unique filter attribute, you can optionally define a [property]Filterable function
Below is an address example
public function addressFilterable($query, $filters) { return $query->where(function($query) use($filters) { foreach($filters as $filter) { switch($filters) { case 'local': $query->orWhere(...); break; case 'foreign': $query->orWhere(...); break; default: $query->orWhere(...); break; } } return $query; }); }
Paginateable and SimplePaginateable
The paginateable columns default to page and per_page just like Laravel's built in pagination
These keys can be overwritten on a per model instance by setting the $paginateablePageKey and $paginateablePerPageKey variables.
If these keys exist, results will be paginated in the response.
If these keys do not exist, the $forcePagination function param will be checked. If true, results will be paginated according to Laravel defaults. If false, results will be retrieved via the get() method.
Ex:
page = 1
per_page = 100
Selectable
The selectable column defaults to selectableFields
This key can be overwritten on a per model instance by setting the $selectableFieldsKey variable
If this field exists, the selectable function will set a static property with these fields to be masked down to upon the call to toArray()
The goal of this trait is to reduce the response size when returning data to a front end application
For security reasons, the relation depth is maxed at 3 and if a relation is not loaded into the model before toArray() is called selectable will abort() out as someone is attempting to load relations dynamically which could cause data leakage
Ex:
selectableFields = id,profile.name,posts.id,posts.name
dykhuizen/laravel-datatable 适用场景与选型建议
dykhuizen/laravel-datatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.34k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「sorting」 「filtering」 「searching」 「selecting」 「paginating」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dykhuizen/laravel-datatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dykhuizen/laravel-datatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dykhuizen/laravel-datatable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Syntactic sugar for PHP's sorting
Nestable table drag and drop sorting.
Modern PHP Sanitization Library
Laravel package for rapid CRUD API scaffolding with filtering, sorting, pagination and CSV export
Official PHP integration for Optidash - AI-powered image optimization and processing API. We will drastically speed-up your websites and save you money on bandwidth and storage.
Alfabank REST API integration
统计信息
- 总下载量: 1.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-28