gbrock/laravel-table 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gbrock/laravel-table

Composer 安装命令:

composer require gbrock/laravel-table

包简介

Table functionality for Laravel models

README 文档

README

Made for Laravel 5 Latest Tag

This package contains flexible ways of rendering Eloquent collections as dynamic HTML tables. This includes techniques for sortable columns, customizable cell data, automatic pagination, user-definable rows-per-page, batch action handling, and extensible filtering (coming soon).

Installation

Require the package in your composer.json:

"gbrock/laravel-table": "dev-master"

Add the service provider to config/app.php and, optionally, the Facade:

'Gbrock\Table\Providers\TableServiceProvider',
...
'Table'      => 'Gbrock\Table\Facades\Table',

Publish the views and config:

php artisan vendor:publish

Usage

In order to render an HTML table of Eloquent models into a view, first create a Table object, passing in your model collection (this could be done in your controller, repository, or any service class):

$rows = User::get(); // Get all users from the database
$table = Table::create($rows); // Generate a Table based on these "rows"

Then pass that object to your view:

return view('users.index', ['table' => $table]);

In your view, the table object can be rendered using its render function:

{!! $table->render() !!}

Which would render something like this:

Basic example

Sorting

To add links in your headers which sort the indicated column, add the Sortable trait to your model. Since no fields are allowed to be sorted by default (for security reasons), also add a sortable array containing allowed fields.

use Gbrock\Table\Traits\Sortable;

class User extends Model {

	use Sortable;
	
    /**
     * The attributes which may be used for sorting dynamically.
     *
     * @var array
     */
    protected $sortable = ['username', 'email', 'created_at'];

This adds the sortable scope to your model, which you should use when retrieving rows. Altering our example, $rows = User::get() becomes:

$rows = User::sorted()->get(); // Get all users from the database, but listen to the user Request and sort accordingly

Now, our table will be rendered with links in the header:

Sortable example

The links will contain query strings like ?sort=username&direction=asc.

Pagination

If you paginate your Eloquent collection, it will automatically be rendered below the table:

$rows = User::sorted()->paginate(); // Get all users from the database, sort, and paginate

Customization

Columns

Pass in a second argument to your database call / Table creation, columns:

 $table = Table::create($rows, ['username', 'created_at']); // Generate a Table based on these "rows"

Cells

You can specify a closure to use when rendering cell data when adding the column:

// We pass in the field, label, and a callback accepting the model data of the row it's currently rendering
$table->addColumn('created_at', 'Added', function($model) {
    return $model->created_at->diffForHumans();
});

Also, since the table is accessing our model's attributes, we can add or modify any column key we'd like by using accessors:

    protected function getRenderedCreatedAtAttribute()
    {
        // We access the following diff string with "$model->rendered_created_at"
        return $this->created_at->diffForHumans();
    }

The default view favors the rendered_foobar attribute, if present, otherwise it uses the foobar attribute.

View

A copy of the view file is located in /resources/vendor/gbrock/tables/ after you've run php artisan vendor:publish. You can copy this file wherever you'd like and alter it, then tell your table to use the new view:

$table->setView('users.table');

gbrock/laravel-table 适用场景与选型建议

gbrock/laravel-table 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.44k 次下载、GitHub Stars 达 76, 最近一次更新时间为 2015 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「html」 「model」 「laravel」 「table」 「sorting」 「filtering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gbrock/laravel-table 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gbrock/laravel-table 我们能提供哪些服务?
定制开发 / 二次开发

基于 gbrock/laravel-table 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 45.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 78
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 76
  • Watchers: 5
  • Forks: 30
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-23