yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 858 forks source link

How to custom query in datatables/ModelDataTable.php #1669

Closed melzangelina closed 6 years ago

melzangelina commented 6 years ago

How to custom query in datatables/ModelDataTable.php

Hi Yajra, may we custom query in ModelDataTable.php? I have CompetencyDataTable.php and I want to join Competency data with Competency Group. I try to change the query, but it gives me error "Method getQuery does not exist.". Where should I put the custom query if I use datatables/ModelDataTable.php? And can you give me example the right query for this case? I use Infyom Generator with datatables when build this.

This is the CompetencyController.php

When I access competency page, it render datatables which has CompetencyDataTable as parameter.

public function index(CompetencyDataTable $competencyDataTable)
    {
        return $competencyDataTable-> render('competencies.index');
    }

This is the original CompetencyDataTable.php

class CompetencyDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */

    public function dataTable($query)
    {
        $dataTable = new EloquentDataTable($query);

        return $dataTable->addColumn('action', 'competencies.datatables_actions');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Post $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Competency $model)
    {
        $query = Competency::query();

        return $this->applyScopes($query);
//        return $model->newQuery();
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '80px'])
            ->parameters([
                'dom'     => 'Bfrtip',
                'order'   => [[0, 'desc']],
                'buttons' => [
                    'create',
                    'export',
                    'print',
                    'reset',
                    'reload',
                ],
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'competency_group_id',
            'name',
            'sort_number'
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'competenciesdatatable_' . time();
    }
}

I try to change in query parameter in datatables

public function dataTable($query)
    {
        $query = DB::table('competencies')
            ->join('competency_groups', 'competencies.competency_group_id', '=', 'competency_groups.id')
            ->select('competencies.*', 'competency_groups.name AS group_name')
            ->get();
        $dataTable = new EloquentDataTable($query);

        return $dataTable->addColumn('action', 'competencies.datatables_actions');
    }

But everytime I run the code, it gives me error "Method getQuery does not exist."

System details

This is my system details

putradongkal commented 6 years ago

Try this code:

public function dataTable($query)
    {
        $dataTable = DB::table('competencies')
            ->join('competency_groups', 'competencies.competency_group_id', '=', 'competency_groups.id')
            ->select('competencies.*', 'competency_groups.name AS group_name')
            ->get();

        return Datatables::of($dataTable)->addColumn('action', 'competencies.datatables_actions');
    }
melzangelina commented 6 years ago

@putradongkal your code works. Thanks!!