在Laravel 5中使用联合查询进行手动分页?

时间:2018-08-20 13:11:35

标签: php laravel-5 pagination union

我正在尝试编写用于手动分页的代码,以便与Laravel 5中的union一起使用。 我尝试使用显示为in this答案的代码,该代码显示了如何在使用联合的情况下使用分页器。

在我的查看页面上,分页显示有分页数据,但分页链接无法正常工作。如果我单击任何页面链接,它将显示主页。请告诉我我在这里做错了吗?

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

这是json文件:

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 个答案:

答案 0 :(得分:1)

我找到了我添加的问题的答案

Paginator::resolveCurrentPath() 

作为其中的最后一个参数

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

LengthAwarePaginator构造函数如下:

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