So I just started working with Laravel Pagination and I'm trying to show results of my database on the pages. In my table, I have the results linked to page_id's and they are linked to a main id. So to get the amount of pages I need, I check how many pages are linked with the main ID.
Like so:
$query = DB::table('table')->where('main','a value');
$paginate = $query->simplePaginate(1);
This outputs 2 pages and that's the right behaviour because I have 2 page_id's linked to the main ID.
Now I try to get the results to show on the pages they are meant to be on. I do it like this:
$get = $query->get();
foreach($get as $something){
$array[]= DB::table('page_results')->where('page_id', $something->page_id)->get();
}
Now here's the problem. On the first page, it shows every result even if it's not linked to page 1, but on the second page it shows the results it should show. For example, I have 2 results linked to the first page and 2 to the second. The first page shows 4 results(results of both pages) and the second one shows 2 results(The right results).
The pagination in my view is the same as in the documentation but with different variables(Documentation code):
<?php foreach ($users as $user): ?>
<?php echo $user->name; ?>
<?php endforeach; ?>
<?php echo $users->links(); ?>
I do not show the results in this yet but in a var_dump in my model
var_dump($array);
What am I doing wrong here? I am extremely confused.
simply use paginate function of laravel in repository
return $this->makeModel()->paginate(10);
and in view page
use render {!! $articles->render() !!} to add pagination