Laravel-datatables: Everything works fine except for the column sort that has relationship.

Created on 15 Jun 2019  路  2Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request


Everything works fine except for the column sort that has relationship.
Screenshot_1

Error:
image

Code snippet of problem


Table:

  • activity_logger

    • id

    • description

    • userId (No FK constraint)

    • created_at

    • updated_at

    • etc...

  • users

    • id

    • name

    • email

    • etc...

Models w/ Relationship:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ActivityLog extends Model
{
    //
    protected $table = 'activity_logger';

    public function user()
    {
        return $this->belongsTo('\App\User', 'userId');
    }

}

DataTable Service Class:

<?php

namespace App\DataTables;

use App\ActivityLog;
use Yajra\DataTables\Services\DataTable;

class ActivityLogsDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables(
            $query->with('user')->select('*')
        );

    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\ActivityLog $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(ActivityLog $model)
    {
        return $model->newQuery()->select(
            'id',
            'created_at',
            'user.name',
            'description'
        );
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->minifiedAjax();
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'id',
            'created_at',
            'user.name',
            'description',
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'ActivityLogs_' . date('YmdHis');
    }
}

System details

  • Windows 7. (using Laragon v4.0.14)
  • PHP Version 7.2.11
  • Laravel Version 5.8
  • Laravel-Datatables Version

    • "yajra/laravel-datatables-buttons": "^4.6"

    • "yajra/laravel-datatables-html": "^4.4"

    • "yajra/laravel-datatables-oracle": "^9.4"

question

All 2 comments

Unknown column user.name because i didn't join or select the users table.
this fix my problem:

public function query(ActivityLog $model)
 {
        return $model->newQuery()->with('user')->has('user')
               ->select('users.id','users.name', 'activity_logger.*');
 }

BTW, thanks for this bad ass package,. really appreciate it.

Was this page helpful?
0 / 5 - 0 ratings