Datatable.js - Laravel: JSON columns are not filtered (orderable)

59 Views Asked by At

I use laravel and the package: yajra/laravel-datatables-oracle on a Mysql database. I also use the mcamara/laravel-localization package to manage the site's different languages.

The eloquent code below displays a datatables.js table: the data is correct and the search engine works. However, I'm unable to apply an order to my columns. Indeed, datatables allows you to filter columns in ascending / descending order, but it doesn't work.

The data in my fields: free_videos.title and categories.title as category are in JSON format ({"en": "My title", "fr": "Mon titre"}.

I've tried filterColumn in all directions, but I can't manage to arrange my columns in ascending or descending order when I click on the header of the column concerned: the arrow indicating the change of display order is displayed and changes state, but the data is not filtered.

Laravel code:

public function active(Request $request)
    {
        $query = FreeVideo::query()->leftJoin('categories', 'free_videos.category_id', '=', 'categories.id')->select('free_videos.id', 'free_videos.title', 'categories.id as category_id', 'categories.title as category');

        return DataTables::eloquent($query)
            ->addColumn('freeVideoTitle', function($row){
                return $row->title; //Json table default lang : "fr" provided ("mcamara/laravel-localization")
            })
            ->editColumn('freeVideoCategory', function($row)
            {
                $category = json_decode($row->category, true);
                return isset($category['fr']) ? $category['fr'] : '';
            })
            ->editColumn('languages', function($row){
                return $row->getTranslations('title');
            })
            ->addColumn('actions', function($row) use ($request){
                $actions['edit'] = ['status' => 1, 'route' => route('backend.freeVideos.edit', $row->id)];
                $actions['destroy'] = ['status' => 1, 'route' => route('backend.freeVideos.destroy', $row->id)];
                return $actions;
            })
            ->make(true);
    }

Javascript code :

table.DataTable({
            language: {url: '/assets/common/js/locales/datatable/fr.json'},
            dom:
                "<'row'" +
                "<'col-sm-6 d-flex align-items-center justify-conten-start'l>" +
                "<'col-sm-6 d-flex align-items-center justify-content-end'f>" +
                ">" +

                "<'table-responsive'tr>" +

                "<'row'" +
                "<'col-sm-12 col-md-5 d-flex align-items-center justify-content-center justify-content-md-start'i>" +
                "<'col-sm-12 col-md-7 d-flex align-items-center justify-content-center justify-content-md-end'p>" +
                ">",
            responsive: true,
            searchDelay: 500,
            processing: true,
            serverSide: true,
            pageLength: 50,
            ajax: ajaxProcessing,
            columns: [
                {
                    data: 'freeVideoTitle',
                    name: 'free_videos.title'
                },
                {
                    data: 'freeVideoCategory',
                    name: 'categories.title'
                },
                {
                    data: 'languages',
                    name: 'langs'
                },
                {
                    data: 'actions',
                    responsivePriority: -1
                },
            ],
            order: [[0, 'desc']], ...
1

There are 1 best solutions below

0
On

Fixed !

  1. $query => categories.title->fr as category
  2. JS => name: 'free_videos.title->fr' and name: 'categories.title->fr'
public function active(Request $request)
    {
        $query = FreeVideo::query()->leftJoin('categories', 'free_videos.category_id', '=', 'categories.id')->select('free_videos.id', 'free_videos.title', 'categories.id as category_id', 'categories.title->fr as category');

        return DataTables::eloquent($query)
            ->addColumn('freeVideoTitle', function($row){
                return $row->title;
            })
            ->addColumn('freeVideoCategory', function($row){
                return $row->category;
            })
            ->editColumn('languages', function($row){
                return $row->getTranslations('title');
            })
            ->addColumn('actions', function($row) use ($request){
                $actions['edit'] = ['status' => 1, 'route' => route('backend.freeVideos.edit', $row->id)];
                $actions['destroy'] = ['status' => 1, 'route' => route('backend.freeVideos.destroy', $row->id)];
                return $actions;
            })
            ->make(true);
    }
columns: [
                {
                    data: 'freeVideoTitle',
                    name: 'free_videos.title->fr'
                },
                {
                    data: 'freeVideoCategory',
                    name: 'categories.title->fr'
                },
                {
                    data: 'languages',
                    name: 'langs'
                },
                {
                    data: 'actions',
                    responsivePriority: -1
                },
            ],