我尝试合并2个查询,并在分页后添加新变量:
$a = Message::where('conversation_id',$id)->where('user_id',$user->id)->where('s_1',1)
->orderBy('created_at','DESC')->get();
$b = Message::where('conversation_id',$id)->where('user_id',$conversation->recipients()->last()->user->id)->where('s_2',1)
->orderBy('created_at','DESC')->get();
$result = $a->merge($b);
$messages = new Paginator($result, $result->count(), 1, 1);
在视图上运行foreach,但渲染是错误的,不要分页,只在一个页面中显示所有结果:
<table class="table table-hover">
@foreach($messages as $i => $message)
<tr>
<td>
<img src="{{$avatar[$message->user->username]}}" class="aoi pull-left" />
<a href="{{route('profile',[$message->user->username])}}">{{ $message->user->name }}</a>
{{ $message->body }}
</td>
</tr>
@endforeach
</table>
{!! str_replace('/?', '?', $messages->render()) !!}
消息var返回正确的参数,但不适用于视图:
{"total":2,"per_page":2,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":2,"data":
url:/ message / 1?page = 2上的显示2行以及/ message / 1和dont paginate。
答案 0 :(得分:0)
我只能假设您的Paginator实际上是LengthAwarePaginator,因为您传递的是项目总数(或者这可能是旧版本的Laravel)。
构建Paginator类的方式:
$messages = new LengthAwarePaginator($result, $result->count(), 1, 1);
如果你看一下Paginator类的定义:
/**
* Create a new paginator instance.
*
* @param mixed $items
* @param int $total
* @param int $perPage
* @param int|null $currentPage
* @param array $options (path, query, fragment, pageName)
* @return void
*/
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
第四个参数是当前页面,您将其作为1传递,使其始终将其分页到第一页。您只需传递null
(或者只是不要避免传递第四个参数),它会尝试通过检查当前请求参数page
来自动解析当前页面。
总之,请将其更改为:
$messages = new LengthAwarePaginator($result, $result->count(), 1);