简体   繁体   中英

Laravel 5 pagination - scrolling to a page containing a specfic record

Is there a way Laravel can tell you which page a record is on? So you can set the browser view to that page?

In general, in order to determine on which page a record would be displayed you'd need first to load all the records as pagination.

It's a bit easier with sorted result lists. You could try to do that using some SQL. You'd need to count number of records that come before the record you need and then calculate the number of page that would contain your record, eg:

//You want to find out on which page a record with ID = 35 will be shown:
$perPage = 10;

// Count number of record where ID is smaller than 35
$count = YourModel::where('id', '<', 35)->count();

// Calculate page number for your record
$pageNumber = floor($count / $perPage) + 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM