When I use the paginate of flask_sqlalchemy, can I limit it also? - limit

My code: DbVote.query.order_by(
(something).desc()).paginate(
page, per_page=5, error_out=False)
For example, I have 1000 pieces of data, and just whant to get top 500, but I have used paginate, how can I to do?

try this:
DbVote.query.order_by((something).desc())
.limit(500).from_self()
.paginate(page, per_page=5, error_out=False)

Related

How I get data like 0 to 50 and 51 to 100 in laravel

I want to get data from database with range like 0 to 50 and 51 to 100, this is possible? if possible please talk me how to do that.
$users = User::all();
I am not able to get clearly what do you want. But I have provided some examples below that might work for you.
$users = DB::table('users')
->whereBetween('id', [1, 50])
->get();
Also try skip and take.
You may use the skip and take methods to limit the number of results returned from the query or to skip a given number of results in the query:
$users = DB::table('users')->skip(50)->take(50)->get();
Alternatively, you may use the limit and offset methods. These methods are functionally equivalent to the take and skip methods, respectively:
$users = DB::table('users')
->offset(50)
->limit(50)
->get();
Hope this helps!
There are a number of options available to you. If you're looking to obtain a working data set of x results at a time and not concerned about the id of the results, then one of the following might be of use:
Pagination
Chunking results
Both of the above will allow you to define the size/number of records returned for your data set.
If you're looking to return records between specific id values, then you'll want to use something like whereBetween on your database queries. The whereBetween clause can be used in conjunction with the pagination and chunking.

Paginate Many to Many

I have a many to many relationship that looks like so
batches -> batch_contributors -> contributors
I am running into an issue when attempting to paginate, I am doing the following:
$contributions = Batch::with('contributors')->where('id', '=' , $batchid)->paginate(10);
Here are samples from my parsed JSON (faker data)
parsed JSON
end of parsed JSON
My problem is the contributor records are listed under the single batch record, so pagination is seeing this and loading all 100 contributors on the first page instead of 10 per page.
How do I get pagination to look at the contributor level instead of the batch level?
You can just invert the call and get contributors where batchId is equal to the one u parse, something like this:
Contributor::with(['batches' => function($query) use ($batchId) {
$query->where('id', $batchId);
}])->paginate(10);
Try this:
Batch::with('contributors', function($query){
$query->take(10); // to take 10 contributors for all the 10 selected Batchs
})->where('id', '=' , $batchid)->paginate(10);
Laravel will get 10 contributors in this case, and try to map them into the batchs, So If those 10 contributors belong to one batch, the rest batchs won't have any contributors.

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

Paginate in elasticsearch

I am new to elasticsearch and at least for the flask extension (flask-elasticsearch) the documentation is very bad. Any help is much appreachiated.
I have a elasticsearch query which looks like this
res = es.search(index="user-index", from_=(page-1)*10, size=10, body={"query": {"match_all": {}}})
I query the user table and want only 10 results. Now I still have to get the actual table rows. So I query them like this
list_of_ids = []
for hit in res['hits']['hits']:
list_of_ids.append(hit["_id"])
search_result = models.User.query.filter(models.User.id.in_(list_of_ids)).paginate(page, 10, False)
as you can see I am using the paginate() function. However, since I passed a list of 10 ids into the query, the paginate function does not know about the total number of results in the search query. So the paginate function can't work this way...
I could just do all the paginate functionality by myself, but I was wondering whether there is a nice way to keep the paginate functionality and somehow link it with the elastic search?
thanks
carl
I found a solution... its possible to just assign the relevant pagination values like
search_result.total = res['hits']['total']
search_result.page = page
cheers

Laravel Paginator getTotal returns null - Need total record count for all pages

I have implemented simple pagination using laravel. It works fine. However, I want to add total number of records and trying to use the method getTotal() but it returns null value.
$records = DB::table('tablename')
->where(condition)
....
....
->simplePaginate(10);
In the view, adding links works fine.
{{$records->links();}}
When I use,
{{$records->getTotal();}}
it returns null.
If I use,
{{$records->count();}}
it returns the count of records for a given page.
Any insights please?
That's how simplePaginate works. From the docs:
If you are only showing "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers on your view.
The simple method is simple because it doesn't need to do the extra, inefficient-on-large-tables count query to obtain the total.
There is method total() in that pagination object. Just call that object. You will get total count.
ex: $records->total();
I had the same problem, what I did was
$records = DB::table('tablename')
->where(condition)
....
....
->paginate(1); //set as 1
$total = $records->getTotal(); //get total
Then returning it like this(I am using ajax that is why I return it as array):
return Response::JSON(array(
'pagination' => (string) $records->links('pagination::simple'),
'total_records' => $total
));

Resources