Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty result set yajra datatable on large tables #858

Closed
ahmadbadpey opened this issue Nov 26, 2016 · 3 comments
Closed

empty result set yajra datatable on large tables #858

ahmadbadpey opened this issue Nov 26, 2016 · 3 comments
Labels

Comments

@ahmadbadpey
Copy link

I have a Question Model by about 30,000 records like this:

class Question extends Model
    {
        use SoftDeletes;

        protected $primaryKey = 'question_id';

        protected $fillable   = ['text', 'code', 'cat', 'answer', 'confirmed', 'is_private', 'email', 'parent', 'expert'];

        public function sub_questions ()
        {
            return $this->hasMany(Question::class, 'parent', 'question_id');
        }

        public function latest_sub_question ()
        {
            return $this->hasOne(Question::class, 'parent', 'question_id')->latest();
        }


    }

To show a list of all questions I'm using yajra datatable laravel package.

This is my js code :

var allQuestionsTable =
$('#allQuestionsTable').DataTable({
                        processing: true,
                        serverSide: true,
                        "bSort": false,
                        "responsive": true,
                        ajax: {
                            url: '{!! route('admin.questionsDatatable') !!}'
                        },
                        columns: [
                            {data: 'checkbox'},
                            {data: 'code', name: 'code'},
                            {data: 'text', name: 'text'},
                            {data: 'created_at'},
                            {data: 'answer', name: 'answer'},
                            {data: 'expert'},
                            {data: 'confirmed'},
                            {data: 'actions'}
                        ]
                    });

And in the back-end I wrote this function:

public function questionsDatatable (Request $request)
        {
            $questions = Question::with('latest_sub_question')->whereNull('parent');

            $datatable = app('datatables')->of($questions)
                ->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$question_id}}" id="Check_{{$question_id}}">');

                //other column modifications comes here

            return $datatable->make(true);
        }

First when my table had A few rows all things worked fine but when I added a large number of new records,questionsDatatable return empty value and cause datatables return an alert error.

what is problem ? and is there any parameter that can use to solve this problem?

@yajra
Copy link
Owner

yajra commented Nov 30, 2016

Have you tried inspecting the ajax request? It should give you an error dump on what's causing the issue.

@yajra yajra added the question label Nov 30, 2016
@ahmadbadpey
Copy link
Author

ahmadbadpey commented Dec 4, 2016

@yajra , Yes I did but just an empty response and 500 Internal Server Error status code returned.

of course between fetching questions and before calling app('datatables') for some reason to iterate result I should use get() method and then use each() method on $questions . in fact my backend code is :

public function questionsDatatable (Request $request)
        {
            $questions = Question::with('latest_sub_question')->whereNull('parent');
             $questions = $questions->get();

			$questions = $questions->map(function ($item, $key) {
				return is_null($item->latest_sub_question) ? $item : $item->latest_sub_question;
			});

			$questions->sortByDesc('created_at');

            $datatable = app('datatables')->of($questions)
                ->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$question_id}}" id="Check_{{$question_id}}">');

                //other column modifications comes here

            return $datatable->make(true);
        }

I do not know that is a reason for the error or not?

@yajra
Copy link
Owner

yajra commented Dec 4, 2016

You are indeed loading all your records which would definitely eat up a lot of your resources. I suggest you remove get. Try something like below.

public function questionsDatatable (Request $request)
{
    $questions = Question::with('latest_sub_question')->whereNull('parent');
    $datatable = app('datatables')->of($questions)
        ->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$question_id}}" id="Check_{{$question_id}}">');
        ->editColumn('latest_sub_question', function($item) {
            return is_null($item->latest_sub_question) ? $item : $item->latest_sub_question;
        })
        //other column modifications comes here

    return $datatable->make(true);
}

@yajra yajra closed this as completed Jan 27, 2017
@github-actions github-actions bot locked and limited conversation to collaborators Nov 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants