ymigval/ laravel-model-datatable-ssp
一个用于无缝集成 Laravel 模型与服务器端 DataTables 的扩展。它提供了一个方便且高效的方式来获取、转换和显示 Laravel 模型中的数据到 DataTables 中。
Requires
- php: >=7.0.0
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^8.10
README
一个用于无缝集成 Laravel 模型与 服务器端 DataTables 的扩展。它提供了一个方便且高效的方式来获取、转换和显示 Laravel 模型中的数据到 DataTables 中。
安装
要开始使用,请通过 Composer 安装此包
composer require ymigval/laravel-model-datatable-ssp
Eloquent 模型使用示例
要使用 DataTables 与 Eloquent 模型,您可以创建一个实例或查询您的模型,并调用带有列映射的 datatable()
方法。
use App\Models\Customer; return (new Customer())->datatable([ 'first_name', 'last_name', 'phone' ]);
或者,您可以调用静态的 datatable()
方法
use App\Models\Customer; return Customer::datatable([ 'first_name', 'last_name', 'phone' ]);
- 将
Customer
替换为您的 Eloquent 模型。 ['first_name' => 'first_name', 'last_name' => 'last_name', 'phone' => 'phone']
:定义您的模型字段的映射。
自定义列
您可以通过提供闭包来在列映射中自定义字段值。
use App\Models.Customer; return Customer::where('type', 'male')->datatable([ 'first_name', 'last_name', 'active' => function ($field, $row) { return ($field) ? 'Yes' : 'No'; } ]);
- $field:包含在该映射中的字段值。
- $row:包含当前行中字段的值。
添加额外列
您可以通过使用闭包来添加额外列
use App\Models\Customer; return Customer::datatable([ 'first_name', 'last_name', function () { return 'additional column #1'; }, function () { return 'additional column #2'; } ]);
字段上下文
定义模型字段在上下文中,以便访问相关数据或执行自定义格式化。
use App\Models\Customer; return Customer::datatable([ 'first_name' => function ($field, $row) { return $field . ' ' . $row->last_name; } ], ['last_name']);
默认情况下,添加到上下文中的字段无法进行搜索和排序。您可以通过向字段添加选项来配置此行为
use App\Models\Customer; return Customer::datatable([ 'first_name' => function ($field, $row) { return $field . ' ' . $row->last_name; } ], ['last_name' => ['orderable' => true, 'searchable' => true]]);
使用查询构建器
您可以通过在查询构建器实例上调用 dataTable()
来使用 DataTable。
use Illuminate\Support\Facades\DB; return DB::table('customers') ->datatable([ 'first_name', 'last_name', 'phone' ]);
转换输出数据
您可以通过指定第三个参数将 datatables 返回值转换为各种格式,如 response
、array
或 json
。
默认情况下,返回一个响应。
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' );
高级使用
使用回调进行列映射
您可以使用回调来动态定义列。
use App\Models\Customer; return (new Customer())->datatable( function () { return ['first_name', 'last_name', 'phone']; } );
联合查询
使用 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']; } );
您还可以向列映射中的字段或上下文中的字段添加别名
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]] );
使用 Eloquent 关系
使用关系时的注意事项
当使用关系时,有一些限制
- 避免在不使用闭包的情况下将相关字段作为列值使用。
- 为了利用相关字段的值,应通过格式化闭包访问它。使用闭包的第二个参数来访问值。请记住,第二个参数包含当前行中字段的值。
- 相关字段无法进行排序或搜索。
请确保将关系中使用的本地键添加到您的列映射或上下文字段中。
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' );
有关更多使用示例,请参阅测试用例。
在您的应用程序中安装 DataTables
在官方 DataTables 文档: https://datatables.net.cn/ 中,您将找到在应用程序中安装库的步骤。
查看服务器端处理示例: https://datatables.net.cn/examples/server_side/simple.html
更新日志
请参阅变更日志以获取有关最近更改的更多信息。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。