Column basic usage

model-grid built-in a lot of the operation of the column, you can use these methods very flexible operation of the column data.

Column attribute

The column object's setAttributes() method is used to add HTML attributes to each row of the current column. A useful scene is to add style to the current column.

$grid->column('title')->setAttributes(['style' => 'color:red;']);

The style() method is based on the setAttributes() method, adding styles directly, such as limiting the width of the column:


List order / orderBY

$grid->model()->orderBy('id', 'desc');

Fixed column

If the table has more fields and squeezes the display of the column, you can use the fixColumns method to set the fixed column.

$grid->fixColumns(3, -2);

The first parameter indicates the first three columns fixed from the beginning, and the second parameter indicates the two columns fixed from the back to the front. (The second parameter can be passed, the default is -1)

Screen Recording 2021-11-23 at 10 56 46

Set column width


Set column color


Setting header help information

$grid->column('title')->help('This column is...');

A question mark question mark icon will appear on the right side of the header of this column. The mouse hover will pop up the set help information.

Default hidden column


This column will be hidden by default and can be turned on in the column selector in the top right corner.

Column Sorting / order

Use the sortable() method to set the current column to a sortable sequence. This allows you to sort / order a column.

$grid->setSortColumns(false); // disable sorting for all columns


$grid->column('created_at')->sortable(false); // to disable

$grid->column('created_at')->sortable(false, "DATE"); // to sort on a value casted as something else

for casting see:

A sort icon will appear in the column header, click to sort


String operations

If the current output data is a string, you can call the method of class Illuminate\Support\Str.

For example, the following column shows the string value of the title field:


Call Str::limit() on title colum.

Can call Str::limit() method on the output string of the title column.


Continue to call Illuminate\Support\Str method:


$grid->title()->ucfirst()->substr(1, 10)->limit(30);

Array operations

If the current output data is a array, you can call the method of class Illuminate\Support\Collection.

For example, the tags column is an array of data retrieved from a one-to-many relationship:


array (
  0 =>
  array (
    'id' => '16',
    'name' => 'php',
    'created_at' => '2016-11-13 14:03:03',
    'updated_at' => '2016-12-25 04:29:35',
  1 =>
  array (
    'id' => '17',
    'name' => 'python',
    'created_at' => '2016-11-13 14:03:09',
    'updated_at' => '2016-12-25 04:30:27',

Call the Collection::pluck() method to get the name column from the array


array (
    0 => 'php',
    1 => 'python',

The output data is still a array after above, so you can call methods of Illuminate\Support\Collection continue.


array (
    0 => 'Php',
    1 => 'Python',

Outputs the array as a string



Mixed use

In the above two types of method calls, as long as the output of the previous step is to determine the type of value, you can call the corresponding type of method, it can be very flexible mix.

For example, the images field is a JSON-formatted string type that stores a multiple-picture address array:


"['foo.jpg', 'bar.png']"

// chain method calls to display multiple images
$grid->images()->display(function ($images) {

    return json_decode($images, true);

})->map(function ($path) {

    return 'http://localhost/images/'. $path;


Extend the column

There are two ways to extend the column function, the first one is through the anonymous function.

Add following code to app/Admin/bootstrap.php:

use OpenAdmin\Admin\Grid\Column;

Column::extend('color', function ($value, $color) {
    return "<span style='color: $color'>$value</span>";

Use this extension in model-grid:


If the column display logic is more complex, can implements with a extension class.

Extension class app/Admin/Extensions/Popover.php:


namespace App\Admin\Extensions;

use OpenAdmin\Admin\Admin;
use OpenAdmin\Admin\Grid\Displayers\AbstractDisplayer;

class Popover extends AbstractDisplayer
    public function display($placement = 'left')
        if (empty($this->value)) {
            $this->value = " "; // bootstrap 5.0.2 bug: not allows empty string

        return <<<EOT
<button type="button"
    class="btn btn-secondary"

And then redister extension in app/Admin/bootstrap.php

use OpenAdmin\Admin\Grid\Column;
use App\Admin\Extensions\Popover;

Column::extend('popover', Popover::class);

Use the extension in model-grid