Donations Appreciated

It's not how much we give but how much love we put into giving. - Mother Teresa

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

EloquentController.php - Count Alias

Id Name Email # of Post Created At Updated At

EloquentController.php

    public function getCount()
    {
        return view('datatables.eloquent.count');
    }

    public function getCountData()
    {
        $users = User::select([
            'users.id',
            'users.name',
            'users.email',
            \DB::raw('count(posts.user_id) as count'),
            'users.created_at',
            'users.updated_at'
        ])->join('posts','posts.user_id','=','users.id')
        ->groupBy('posts.user_id');

        return Datatables::of($users)->make(true);
    }

Javascript

    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'https://datatables.yajrabox.com/eloquent/count-data',
        columns: [
            {data: 'id', name: 'users.id'},
            {data: 'name', name: 'users.name'},
            {data: 'email', name: 'users.email'},
            {data: 'count', name: 'count', searchable: false},
            {data: 'created_at', name: 'users.created_at'},
            {data: 'updated_at', name: 'users.updated_at'}
        ]
    });