1
votes

I am trying to write code for manual pagination to work with union in laravel 5. I am tried to use code shown in this answer which shows how to use the paginator in the context of using a union.

On my view page pagination is showing with paginated data but pagination links are not working right. if i click any page link it shows home page. please tell me what i am doing wrong here?

Controller : UnionsController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Input;
use App\Post;
use DB;

class UnionsController extends Controller
 { 
 public function index(){
 $page = Input::get('page', 1);
 $paginate = 5;
 $first = DB::table('movieinfo')
  ->select('id','movie_name','poster','movie_name1','year','type','season','imdb')
 ->where('id', '>', 100);

$items = DB::table('tvshows')
->select('id','show_name','poster','show_name1','year','type','season','imdb')
->where('id', '>', 100)
->union($first)
       ->orderBy('id','desc')
       ->get();

$slice = array_slice($items->toArray(), $paginate * ($page - 1),  $paginate);
return $result = new \Illuminate\Pagination\LengthAwarePaginator($slice, count($items), $paginate, $page);

return view('umoviehub.index')->with('data', $result);
}  
}

view : index.blade.php

@extends('layouts.app')

@section('content')

<div class="row mt-3 mt-3 mb-3 no-gutter" style="">
@foreach ($data as $value)
<div class="col-6 col-sm-4 col-md-3 col-lg-2 mt-3" style=" display:inline-block; height:270px;">

<img class="rounded " src="/storage/{{$value->poster}}"  width="100%" height="90%">
</div>    
@endforeach
</div>    
{{$data->links()}} 
@endsection

this is json file :

current_page 24
data    
0   {…}
1   {…}
2   {…}
3   {…}
4   {…}
first_page_url  "/?page=1"
from    116
last_page   196
last_page_url   "/?page=196"
next_page_url   "/?page=25" 
path    "/"
per_page    5
prev_page_url   "/?page=23"
to  120
total   980
1

1 Answers

1
votes

I found answer for my question i added

Paginator::resolveCurrentPath() 

as last parameter in

$result = new \Illuminate\Pagination\LengthAwarePaginator($slice, count($items), $paginate, $page, ['path' => Paginator::resolveCurrentPath()]);

as LengthAwarePaginator constructor looks like:

public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])