Laravel-datatables: Orering don't work with relationship column

Created on 18 Feb 2020  路  8Comments  路  Source: yajra/laravel-datatables

Sorry for my english
I'm make column Role for table Users. Role is relationship with table User.
Every columns is orderable and work fine, except column role. This column showing data of role, but don't work order. Error DataTables warning: table id=users-table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Search work fine.

My code

    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('action', function($user){
                return view('users.includes.action', compact('user'));
            })
            ->addColumn('roles', function (User $user) {
                return $user->roles->map(function($role) {
                    return str_limit($role->name, 300, '...');
                })->implode('<br>');
            });
    }

$model->newQuery()->with('roles:id,name');

    protected function getColumns()
    {
        return [
            Column::make('id'),
            Column::make('name')->title('袠屑褟'),
            Column::make('email'),
            Column::make('created_at')->title('袛芯斜邪胁谢械薪')->width(60),
            Column::make('roles','roles.name')->title('袪芯谢褜'),
            Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(300)
                ->addClass('text-center'),
        ];
    }

Thanks for help

  • PHP Version 7.2.0
  • Laravel Versionla 6.11.0
  • Laravel-Datatables Version 1.5

All 8 comments

If you follow the diagnosis steps from https://datatables.net/manual/tech-notes/7 what is the response from the request?

Is your name column nullable? I have had issues with ordering on relations if there are null values, in which case you can solve it by writing your own orderColumn() function for that column:
https://yajrabox.com/docs/laravel-datatables/master/order-column

Thanks, but for me it's doesn't work.
I tried use orderColumn but it's don't work too
Column is not nullable.
Data in table work, search work, sort by click on table header Role don't work.
In console error 500

Console error 500 indicates there was something server-side that went wrong. Take a look at your server logs (apache error logs, if it's apache) and see what is causing the 500 error. Your issue will likely be clear from that.

In log i have error
[18-Feb-2020 17:04:58 UTC] PHP Fatal error: Method Illuminate\View\View::__toString() must not throw an exception, caught Facade\Ignition\Exceptions\ViewException: Missing required parameters for [Route: users.show] [URI: users/{user}]. (View: F:\Server\domains\dnk.loc\resources\views\users\includes\action.blade.php) in F:\Server\domains\dnk.loc\vendor\yajra\laravel-datatables-oracle\src\Utilities\Helper.php on line 0

i remove $user from action.blade.php

and now i have other error
DataTables warning: table id=users-table - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

Ordering after error is working, but table is empty.
Screen before ordering
image

Screen after ordering
image

if i understand right, my User model after ordering is removing

I would give orderColumn() a second try at this point.

Something along the lines of:

    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('action', function($user){
                return view('users.includes.action', compact('user'));
            })
            ->addColumn('roles', function (User $user) {
                return $user->roles->map(function($role) {
                    return str_limit($role->name, 300, '...');
                })->implode('<br>');
            })->orderColumn('roles', function ($query, $order) {
                    $query->orderBy('roles.name', $order);
            });
    }

Not sure if it will know roles.name column, you may need to do a join first. Something like this...

$query->join('roles', 'users.role_id', '=', 'roles.id')->orderBy('roles.name', $order);

Thanks for help, but it still doesn't work. Same error. And with join too

I have had a similar issue. I was using a base model with a related Entry that has a related User and a related Role, the DataTable can sort booth names. Problem is that if i do so the data in the addColumn method is not of type Entry in m case but of type User if i sort by user name or Role if i sort by role name. What happens is that it does get the atributes/row of these related models and does cast it into the model i used as type for the callback. This is because it is $row, not $model. This was my code.

return DataTables::eloquent($adminconfig->entries()->with('user', 'serverGroup'))
                ->addColumn('name', function (AdminConfigEntry $entry) {
                    return view('admin.adminconfigs.parts.name', [
                        'user' => $entry->user,
                    ])->render();
                })
                ->addColumn('group', function (AdminConfigEntry $entry) {
                    return $entry->serverGroup->name;
                })
                ->addColumn('actions', function (AdminConfigEntry $entry) use ($adminconfig) {
                    return view('admin.adminconfigs.parts.actions', [
                        'adminConfig' => $adminconfig,
                        'entry' => $entry,
                    ])->render();
                })
                ->rawColumns(['name, 'actions'])
                ->make();

As mentioned in the docs, using $adminconfig->entries()->select('admin_config_entries.*') as the query, makes sure that you get the correct row data to be cast to a model. Make sure to select the correct data when working with relations and DataTables.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sangnguyenplus picture sangnguyenplus  路  3Comments

nasirkhan picture nasirkhan  路  3Comments

hari-web picture hari-web  路  3Comments

jgatringer picture jgatringer  路  3Comments

alejandri picture alejandri  路  3Comments