Search code examples
phplaraveldatatablesyajra-datatable

Laravel Datatables getting net::ERR_EMPTY_RESPONSE error when adding extra column


I'm using DataTable 1.10.21 with Laravel 7. I'm trying to populate all of the users data, except my user table has an additional column named group_id, which define the user group (ie. admin, manager, user etc)

Right now my user model has this relationship:

public function group() {
        return $this->belongsTo(Group::class);
    } 

When I call

App\User::select(*)->with('group')->get();

I'll get something like this which is good:

App\User {#105150
         id: 1,
         group_id: 1,
         name: "Admin",
         email: "admin@admin.com",
         email_verified_at: "2020-01-01 01:01:01",
         ...
         deleted_at: null,
         group: App\Group {#105096
           id: 1,
           name: "admin",
         },
       }
       ...

In my UserController I have this function:

public function datatable() {
        $data = User::select("*")->with('group');
        $dt = Datatables::of($data)->make(true);
        \Log::debug($dt); //this works!!
        return $dt;
    }

I can use tinker to get $data and $dt without any problem.

My view contains the following javascript:

$(function() {
               $('#table').DataTable({
               processing: true,
               serverSide: true,
               ajax: '{{ url('user/datatable') }}',
               columns: [
                        { data: 'id', name: 'id' },
                        { data: 'name', name: 'name' },
                        { data: 'group_id', name: 'group_id' }, /* this won't work */
                        { data: 'group.name', name: 'group.name' }, /* this won't work either */
                        { data: 'created_at', name: 'created_at' },
                        { data: 'email', name: 'email' }
                     ]
            });
         });
<table class="table table-bordered" id="table">
               <thead>
                  <tr>
                     <th>ID</th>
                     <th>Name</th>
                     <th>Group ID</th>
                     <th>Group Name</th>
                     <th>Creation Date</th>
                     <th>Email</th>
                  </tr>
               </thead>
            </table>

Neither of the group_id or group.name col will work. (net::ERR_EMPTY_RESPONSE error even $dt has response) but if I remove both columns then it works fine.

Please advise.

Edit: May 29 2020

I digged a little deeper and here's what I have found:

If I use my local test domain (ie. myproject.test)

  • If I remove some other columns (ie. created at, email) it worked
  • If I include all columns (ie. id, name, group_id, group.name, created_at, email) I got the ERR_EMPTY_RESPONSE error

If I use 127.0.0.1:8000

  • it worked even if I include all columns!!!!

Can someone please explain to me why??


Solution

  • Yajra\Datatables is one of those areas I personally prefer to use raw sql.

    With your above requirements I'd see it like so

    $data = DB::select("
    Select
      a.id as 'users.id',
      a.name as 'users.name',
      b.id as 'group.id',
      b.name as 'group.name',
      a.created_at as 'users.created_at',
      a.email as 'users.email'
    FROM
      users as a
    LEFT JOIN 
      groups as b
    ON
      a.group_id = b.id");
    
    $data = Datatables::of($data)->make(true);
    return $data;
    

    Then in your columns section of your datatable initialization code just update the data with "users.id", "users.name", etc (same with the name fields).

    Yajra is great, but I ran into issues with it and eloquent myself. Hence why I just went back to raw sql, havent had a problem since.

    Edit - with new data: Then I'd suggest you post your .env file that has been loaded to the server in question. Are you doing all of this locally or do you have a seperate server you are having problems on?