本书未发布

数据列表

未匹配的标注

文档尚未来得及翻译,欢迎以改进的方式提交译文。

Columns

Introduction

In each model's config file you must specify a columns option. This should be an array of the columns that you wish to display in a model's results table. Columns can be made up of any of the following:

You can control the column's header through the title option, the sort_field that will be used if you sort by that columns, and any custom output you want for that column.

Simple Columns

If you want to just output the value of your database table, you can do this:

'columns' => array(
    'id',
    'name',
    'price',
)

The values have to match the name of the column in your database. If you are using a more complex arrangement like in the next few sections, you'll need to make sure that the column's name as the key of your array item like this:

'columns' => array(
    'id' => array(
        'title' => 'ID'
    ),
)

If you're deriving this column from another table or a custom select, this key will be used by Administrator as the column's alias.

For the remainder of this page, the 'columns' => array() portion will be kept out of examples. All of the subsequent examples are supposed to be in the 'columns' array, if it's not screamingly obvious.

Column Headers

If you want to control the output of a column's header, set the title option:

'id' => array(
    'title' => 'ID'
)

Visible Option

The visible option lets you determine if a column should be present. The default value is boolean true. Passing in boolean false will hide the column. You can also pass in a closure that accepts the current data model as the single parameter. You can return a truthy value if you want to show the column for that user, or you can return a falsey value if you hide it.

'secret_info' => array(
    'title' => 'Secret Info',
    'visible' => function($model)
    {
        return Auth::user()->hasRole('super_admin');
    },
),

Using Accessors

Eloquent Accessors can also be used as column values. For instance, if you have a column called salary and an accessor in your Eloquent model that looks like this:

public function getFormattedSalaryAttribute()
{
    return '$'.number_format($this->getAttribute('salary'), 2);
}

You would be able to reference formattted_salary as the column's key like this:

'formatted_salary' => array(
    'title' => 'Formatted Salary'
)

Note: You won't be able to sort accessor columns until you define a sort_field!

Setting the Sort Field

If you're using an accessor, you may also want to define a sort_field that Administrator can use to sort it. This is required because an accessor doesn't exist on the model's database table. All you'd need to do is reference an item on that table like this:

'formatted_salary' => array(
    'title' => 'Formatted Salary',
    'sort_field' => 'salary',
)

Unsortable Columns

If you want to disable sorting for a column, you can set the sortable option to false:

'image' => array(
    'title' => 'Image',
    'output' => '<img src="/uploads/products/resize/(:value)" height="100" />',
    'sortable' => false,
)

Custom Selects

If you're dissatisfied with using your model's standard columns and accessors, you also have the ability to create a column as a custom select statement. Any valid SQL SELECT statement works here. This is useful if you want to use things like SELECT functions. This also comes with a (very) slight performance boost over using accessors since all the work is happening in the SQL.

When you define a custom select, you need to prefix any column in the table with (:table).. This is necessary because the query that is performed to get your model's result set often joins together a number of different tables. This is what a custom select option might look like:

'good' => array(
    'title' => 'Is Good',
    'select' => "IF((:table).is_good, 'yes', 'no')",
)

Here the good key will be the column's alias, so you can name it anything you want. If is_good is 1 for a row, it will show as 'yes'. If it is 0, it will show as 'no'.

Relationship Columns

For a more in-depth look at relationship columns, check out the relationship columns docs

In any moderately complex database, a table might have columns that represent an ID on another table. Most of the time it's fairly useless to display this ID to an admin user because numbers mean more to a computer than to a human. Alternatively, a relationship may not be represented on a model's table at all, but instead on a pivot table that connects two tables, or as a column on another model's table.

If you want to display related columns, you can provide a relationship option. The value of this option has to be the name of the Eloquent relationshp on your model. In addition to this, you need to provide a select option that Administrator will use to grab values from the relationship table. So if you have a Director model and you want to count the number of films he's been involved in, you could do something like this:

'num_films' => array(
    'title' => '# Films',
    'relationship' => 'films', //this is the name of the Eloquent relationship method!
    'select' => "COUNT((:table).id)",
)

Any SQL grouping function will work in the select statement. Much more is possible with the relationship option, so check out the relationship columns docs for more detail.

Custom Outputs

If you want your column to show more than just text, you can use the output option. This can either be a string or an anonymous function.

If you provide an anonymous function, the argumentis available are the relevant column's value from the database, and the current model. For instance, if you are using a color field and you want to clearly show the admin user what color a row is, you can do this:

'hex' => array(
    'title' => 'Color',
    'output' => function($value, $model)
    {
        return '<div style="background-color: ' . $value . '; width: 200px; height: 20px; border-radius: 2px;"></div>';
    },
),

Alternatively, you can also pass a string to the output option and Administrator will replace the string (:value) with the row's value for this column.

'hex' => array(
    'title' => 'Color',
    'output' => '<div style="background-color: (:value); width: 200px; height: 20px; border-radius: 2px;"></div>',
),

Or maybe you're using an image field and you want to display the image in your result set. In that case you'd do:

'banner_image' => array(
    'title' => 'Banner Image',
    'output' => '<img src="/uploads/collections/resize/(:value)" height="100" />',
),

Custom outputs are available for all column types, not just columns that are on the model's table.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
Summer
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~