简体   繁体   English

在laravel 5 中使用联合查询手动分页?

[英]Manual Pagination with union query in laravel 5?

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.我正在尝试编写用于手动分页的代码,以便在 laravel 5 中使用联合。我尝试使用答案中显示的代码,该代码显示了如何在使用联合的上下文中使用分页器。

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控制器: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视图: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 :这是 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

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:因为 LengthAwarePaginator 构造函数看起来像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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