Wrong pagination link count laravel - php

I need to skip the first n records, say 5 from a table and paginate the rest of it. The following is the code that i am using.
$skipNewsListArr = array(1,2,3,4,5);
$wrongPaginationLinks = DB::table('news')
->whereNotIn('news_id',$skipNewsListArr)
->orderBy('news_date','desc')
->paginate(5);
But the above code outputs the pagination links corresponding to the whole table. How can i achieve pagination by skipping records based on some condition.

Related

Pagination pages problem collection / array

I've used the query builder instead of the normal select(I had to, I have added pagination).
query builder example :
$dataGlobal = DB::table('articles')
->orderBy('articleDate', 'desc')
->orderBy('articleHour', 'desc')
->orderBy('articleMinute', 'asc')
->orderBy('articleSeconde', 'asc')
->limit(150)
->paginate(15);
With this code, normally I shoud have 10 pages no more, but instead of the 10 pages I have 23. Counting from page 5 to 23, all of these pages aren't working with this error code; Undefined array key 0
For the pagination I'm using the bootstrap 5 blade pagination (vendor folder)
My question is why am I having all of theese pages ? The weird thing is I have this problem in one page only which is my home page, but in the other ones I don't have this problem at all everything is good
Maybe I didn't give enought details let me know if you guys need me to add something.
Actually, you can't use limit with pagination.
see this answer: Using limit parameter in paginate function

Group By and pagination not working togather laravel

$campaigns = Campaign::orderBy('id','DESC')->paginate(2)->groupBy('offer_id');
results returning 2 rows but no pagination links.
You should use groupBy first then paginate in Laravel as its chaining of method calling your last method will be given result as two records without pagination
Campaign::orderBy('id','DESC')->groupBy('offer_id')->paginate(2);

increment the view counter while fetching in one single query laravel

I have a table named advertisments which has views column.
I want to fetch the data from advertisments table and increment each row's views in a single query is it possible ?.
The following code is what I wrote to achieve the desired result but the views aren't updating.
Advertisement::with('sponsor')
->whereDeleted(false)
->whereHeader(false)
->orderBy('views')
->raw("update views ".DB::raw('views + 1'))
->get();

yii2 dataprovider get all filtered data

I have grid view of student table when I filter table, for that instant I can get filter data by giving following
$allData=$dataProvider->getModels();
This $allData contains filtered data.
If I have 50 records, when I filter them I get 30 records, but this $allData shows only 20 records due to pagination limit 20.
so How do I get all my 30 filtered records into a variable as well as pagination?
if I set pagination size =0, then I will get all data, but I want pagination too.
So, how do I solve this?
If youre using DataProvider, you have $query as well. So using:
$models = $query->all();
will return all filtered models, and DataProvider will still have pagination.

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, e.g.:
//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;

Categories