Laravel-datatables: sorting not working in server side datatables

Created on 17 Aug 2017  ·  3Comments  ·  Source: yajra/laravel-datatables

I m using yajra datatables with ajax and server side script. My search works okay but when i click on sort button nothing will happen.

My controller

$suppliers = DB::table("suppliers")
                        ->select(
                            'suppliers.id', 
                            'suppliers.created_at', 
                            'suppliers.email',
                            'suppliers.status', 
                            'companies.id as company_id',
                            'companies.name', 
                            'company_categories.title',
                            DB::raw('
                                (select COUNT(promotions.company_id) FROM promotions WHERE promotions.status = "pending" AND promotions.company_id = companies.id) as total')
                        )
                        ->leftJoin('companies','companies.supplier_id','=','suppliers.id')
                        ->leftJoin('company_sub_categories','company_sub_categories.id','=','companies.sub_category_id')
                        ->leftJoin('company_categories','company_categories.id','=','company_sub_categories.company_category_id')
                        ->groupBy('suppliers.id', 'suppliers.created_at', 'suppliers.first_name','suppliers.last_name', 
                            'suppliers.phone','suppliers.email','suppliers.status','suppliers.image_path','companies.name','companies.id','companies.name','company_categories.title')
                        ->orderBy('suppliers.id','DESC');

        return Datatables::of($suppliers)
            ->add_column('next_payment_date', function($suppliers){
                return date(config('constant.DATE_FORMAT'), strtotime($suppliers->created_at));
            })

            ->filter(function ($query) use ($request) {
                if (@$request->search['value']) {

                    $query->where('suppliers.email', 'like', "%{$request->search['value']}%")
                    ->orWhere('companies.name', 'like', "%{$request->search['value']}%")
                    ->orWhere('company_categories.title', 'like', "%{$request->search['value']}%")
                    ->orWhere('suppliers.id', 'like', "%{$request->search['value']}%");
                }                
            })

            ->make(true);


my js

$(function() {

  oTable = $('#supplier').DataTable({
        "processing": true,
        serverSide: true,
        "pageLength": 25,
        ordering: true,
        searching: true,
        "sDom":"tpr",
        "dom": 'difrtp',
        ajax: '{!! route('get-supplier') !!}',
        columns: [
            { data: 'name', name: 'name' },
            { data: 'email', name: 'email' },
            { data: 'title', name: 'title' },
            { data: 'created_at', name: 'created_at' },
            { data: 'status', name: 'first_name' },
            { data: 'next_payment_date', name: 'next_payment_date' },
            { data: 'action', name: 'action' }
        ]
    });
}

Most helpful comment

You have an order by command. It will override all sorting functions. Try removing that and comment your result.

->orderBy('suppliers.id','DESC');

All 3 comments

You have an order by command. It will override all sorting functions. Try removing that and comment your result.

->orderBy('suppliers.id','DESC');

solved. thanks @ChaosPower

I was having the same problem with this query:

return self::select($select)
            ->where('language', app()->getLocale())
            ->leftJoin('content_translations', 'content_translations.content_id', 'contents.id')
            ->latest();

I changed my by adding the get() function at the end, this solved my problem also.

return self::select($select)
            ->where('language', app()->getLocale())
            ->leftJoin('content_translations', 'content_translations.content_id', 'contents.id')
            ->latest()
            ->get();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mantrax314 picture mantrax314  ·  15Comments

phainv picture phainv  ·  16Comments

Yahav picture Yahav  ·  16Comments

Arkhas picture Arkhas  ·  15Comments

faisalhilmi picture faisalhilmi  ·  18Comments