## Datatables

LAP uses the Laravel Datatables package for searching, sorting, and pagination. Some of the UI was slightly tweaked, but its core functionality in terms of PHP remains the same.

To give you an idea of how this is utilized, we can take a look at the `ActivityLogController->index()` method:

    public function index(Builder $builder)
    {
        if (request()->ajax()) {
            $activity_logs = app(config('lap.models.activity_log'))->with('user');
            $datatable = datatables($activity_logs)
                ->editColumn('actions', function ($activity_log) {
                    return view('lap::activity_logs.datatable.actions', compact('activity_log'));
                })
                ->rawColumns(['actions']);

            return $datatable->toJson();
        }

        $html = $builder->columns([
            ['title' => 'Created At', 'data' => 'created_at'],
            ['title' => 'Message', 'data' => 'message'],
            ['title' => 'User', 'data' => 'user.name'],
            ['title' => '', 'data' => 'actions', 'searchable' => false, 'orderable' => false],
        ]);
        $html->setTableAttribute('id', 'activity_logs_datatable');

        return view('lap::activity_logs.index', compact('html'));
    }

First off, this is saying that if the request is AJAX, then it should return the contents of the datatable itself. Otherwise, it will be returning to contents of the index view.

Note the use of `editColumn` and `rawColumns` here, as that column in particular is using a blade template in order to render the column contents. This is important, because putting HTML in controllers is pure evil.

Next, we've specified which columns to display via `$builder->columns`. This is fairly straight forward, but please refer to the Laravel Datatables Column Docs if you need more information on what options are available.

Finally, we need to use `$html->setTableAttribute` so that the state of this particular datatable can be saved. This allows the table to remember exactly where the user left off, so that they aren't frustrated when they return to the index page.