ajaxlaravellaravel-8yajra-datatable

Laravel Yajra datatables ajax serverside slow down page load


I'm using Yajra datatables with serverside true, but page load very slow (afraid because of distinct count). I tried solution here by removing ->get() and it load faster but this causing another problem, where the input searching (smart: true) return error, not functioning. Anyone can help?

Here is my code:

$links = Link::with('owner')
->withCount(['clicks as uniqueClicks' => function($q) {
      $q->select(DB::raw('count(distinct(ip_address))'));
}])
->where('account_id', $account_id)
->orderBy('created_at','desc')
->get(); 

return Datatables::of($links)->make();

Is it possible to optimize this code? maybe change from select raw distinct to groupBy? or do this more on eloquent way?


Solution

  • My mistake for not stating the full errors and in the error mentioned about laravel relationship column not found.

    Here is the documentation https://yajrabox.com/docs/laravel-datatables/master/relationships

    columns: [
        // columns according to JSON
        { data: '' },
        { data: 'title' },
        { data: 'user_name', name: 'user.manage.name' }, <-- name = relationship column
        { data: 'link' },
        { data: 'created_at', "searchable": false },
        { data: 'action' }
      ],
    

    So removing the ->get() do make your page load faster.

    $links = Link::with('owner')
    ->withCount(['clicks as uniqueClicks' => function($q) {
      $q->select(DB::raw('count(distinct(ip_address))'));
    }])
    ->where('account_id', $account_id)
    ->orderBy('created_at','desc');
    
    return Datatables::of($links)->make();