yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 858 forks source link

Default total records (length) to API #1597

Open SergioFloresG opened 6 years ago

SergioFloresG commented 6 years ago

Feature request

Hello, we are developing an api and we are certainly interested in using your component.

We have consulted the documentation but there is no element that allows us to define in the configuration the amount of elements to obtain by default.

The application is an API, it does not have direct interaction with a site or Javascript.

'search' => [
        /*
         * Sets the amount of item to obtain by default.
         * It must be a positive integer.
         * 
         * 0: without limit
         */
        'length' => 20,
datatables()->eloquent( $query )
            ->setDefaultLength(30)
            ->make()

Regards

System details

SergioFloresG commented 6 years ago

Good! I will create a branch for this feature. Or your created the branch?

THPAdmin commented 11 months ago

Did this ever get added? I was doing some debugging on a json endpoint, and it pulls 9,300 records and freezes my browser, lol. All an attacker would have to do is keep refreshing the endpoints on multiple computers and it would probably bring our servers to a crawl. Rather than a "default" length, I was more looking for a "maximum limit".

yajra commented 11 months ago

I think this was not implemented. Actually, they can just set the length request to -1 to get all the records.

Given this, maybe you can create a middleware to set the maximum length overriding the request?

request()->merge(['length' => -1]); // Get all records

if (request('length') > 100) {
  request()->merge(['length' => 100]);
}

Feel free to submit a PR, I also think this would be a great feature to implement.