定制 stevebauman/eloquenttable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

stevebauman/eloquenttable

最新稳定版本:v1.1.7

Composer 安装命令:

composer require stevebauman/eloquenttable

包简介

An HTML table generator for laravel collections

README 文档

README

Travis CI Code Climate Scrutinizer Code Quality Latest Stable Version Total Downloads License

🚨 Abandoned 🚨

I'd heavily recommend the use of Orchestra/HTML over this package, as their solution is further tested, complete, and much more robust. This package will receive no further updates.

If you would like to be a maintainer of this package, please contact me at steven_bauman@outlook.com, or create an issue.

Thank you!

Description

Eloquent table is an HTML table generator for laravel collections.

Installation

Include the package in composer.json:

"stevebauman/eloquenttable": "1.1.*"

Now perform a composer update.

Laravel 4

Include the service providers in the bottom app/config/app.php config file:

'Stevebauman\EloquentTable\PaginationServiceProvider',
'Stevebauman\EloquentTable\EloquentTableServiceProvider',

Publish the config file (optional)

php artisan config:publish stevebauman/eloquenttable

Laravel 5

Include the service providers in the bottom config/app.php config file:

'Stevebauman\EloquentTable\EloquentTableServiceProvider',

Publish the config file (mandatory in Laravel 5)

php artisan vendor:publish

You're good to go!

Note: The showPages() method below in unavailable in Laravel 5 due to the pagination changes. You'll need to display your pages manually using the render() method shown here: http://laravel.com/docs/5.0/pagination#usage

Usage

Insert the trait on your model:

class Book extends Eloquent {

    use \Stevebauman\EloquentTable\TableTrait;

    protected $table = 'books';

}

Grab records from your model like usual:

$books = Books::get();

return view('books.index', compact('books'));

Inside your blade view, we just specify the columns we want to show, and then call the render method:

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By'
    ))
    ->render() 
!!}
Handling relationship values using means($column, $relationship):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user.first_name')
    ->render()
!!}

The model books, needs to have a user method defining it's relation for this to work.

You must also use 'dot' notation to indicate the relationship.

Customizing the display of the column value using modify($column, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modify('owned_by', function($user, $book) {
        return $user->first_name . ' ' . $user->last_name;
    })
    ->render() 
!!}

Using modify, we can specify the column we want to modify, and the function will return the current relationship record (if the column is a relationship), as well as the current base record, in this case the book.

Customizing the attributes of each cell of a column using modifyCell($column, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modifyCell('owned_by', function($user) {
        return array('class' => $user->role);
    })
    ->render() 
!!}

Using modifyCell, we can specify the column of the cell we want to modify, and the function should return an array of attributes to be added to the cell.

Customizing the attributes of each row in the table using modifyRow($name, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modifyRow('mod1', function($user) {
        return array('id' => 'user-'.$user->id);
    })
    ->render() 
!!}

Using modifyRow, we can add named modifications ('mod1' in our previous example), and the function should return an array of attributes to be added to each row.

With eloquent-table, we can also generate sortable links for columns easily:

In your controller:

$books = Book::sort(Input::get('field'), Input::get('sort'))->get();

In your view:

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->sortable(array('id', 'title'))
    ->render()
!!}

A link will be generated inside the column header that will be clickable. The HTML generated will look like:

<a class="link-sort" href="http://www.example.com/books?field=id&amp;sort=desc">
    ID <i class="fa fa-sort"></i>
</a>
What about if we want to combine this all together, with pagination and sorting? Easy:

In your controller:

$books = Book::sort(Input::get('field'), Input::get('sort'))->paginate(25);

return view('books.index', compact('books'));

In your view:

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
        'publisher' => 'Publisher',
    ))
    ->means('owned_by', 'user')
    ->modify('owned_by', function($user, $book) {
        return $user->first_name . ' ' . $user->last_name;
    })
    ->means('publisher', 'publisher')
    ->modify('publisher', function($publisher, $book) {
        return 'The publisher of this book: '. $publisher->name;
    })
    ->sortable(array('id', 'title'))
    ->showPages()
    ->render()
!!}
What if I want to generate a table for a relationship?:

In your controller:

$book = Book::with('authors')->find(1);

return view('book.show', compact('book'));

In this case, the book is going to have many authors (hasMany relationship)

In your view:

{!!
    $book->authors->columns(
        'id' => 'ID',
        'name' => 'Name',
        'books' => 'Total # of Books'
    )
    ->means('books', 'num_of_books')
    ->render()
!!}

Keep in mind, we cannot paginate the table, or provide sortable columns on relationships. If you need this, grab it separately:

In your controller:

$book = Book::find(1);

$authors = Authors::where('book_id', $book->id)->paginate(25);

return view('books.show', array(
    'book' => $book,
    'authors' => $authors,
));

In your view:

{!!
    $authors->columns(array(
        'name' => 'Name',
    ))->render()
!!}
Customizing table attributes using attributes($attributes = array())
{!!
    $authors->columns(array(
        'name' => 'Name',
    ))
    ->attributes(array(
        'id' => 'table-1',
        'class' => 'table table-striped table-bordered',
    ))
    ->render()
!!}
Showing your pages somewhere else:

Just don't call the showPages() method on the collection and put your pages somewhere on your page like you would regularly do.

{!!
    $authors->columns(array(
        'name' => 'Name',
    ))
    ->attributes(array(
        'id' => 'table-1',
        'class' => 'table table-striped table-bordered',
    ))
    ->render()
!!}

<div class="text-center">{!! $authors->appends(Input::except('page'))->links() !!}</div>
Why is a pagination service provider required?

When calling paginate() on your models and/or collections, a different collection instance is returned. Unfortunately the only solution is to override the default paginator instance. However, this paginator extends laravel's built in paginator, so absolutely no functionality is removed or lost.

stevebauman/eloquenttable 适用场景与选型建议

stevebauman/eloquenttable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.62k 次下载、GitHub Stars 达 46, 最近一次更新时间为 2014 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 stevebauman/eloquenttable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 50.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 14
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 46
  • Watchers: 4
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-26