我使用的是LengthAwarePaginator,它将从Twitter api获得的结果分页。第一页工作得很好但是当我通过分页链接导航到任何其他页面时,它不会给出任何结果。
路线:
Route::any('/search', ['uses' => 'TweetController@getSearch', 'as' => 'search']);
TweetController:
public function getSearch(Request $request)
{
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$perPage = 6;
$tweetArray = Twitter::getUserTimeline(['screen_name' => $request['search'], 'count' => $request['count']]);
$tweetPerPage = array_slice($tweetArray, (($currentPage-1) * $perPage), $perPage, true);
$tweets = new LengthAwarePaginator($tweetPerPage, count($tweetArray), $perPage, $currentPage, ['path' => $request->url()]);
return view('tweets', ['tweets' => $tweets]);
}
tweet.blade.php:
<div class="row">
<form action="{{ route('search') }}" method="post">
<div class="col-md-5 col-md-offset-3">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Search using Twitter Handle" name="search" aria-describedby="basic-addon1">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="number">How many?</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#">10</a></li>
<li><a href="#">20</a></li>
<li><a href="#">30</a></li>
</ul>
</div>
</div>
</div>
<div class="col-md-1">
<button type"submit" class="btn btn-info">Search</button>
</div>
<input type="hidden" name="count" value="10">
</form>
</div>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('.dropdown-menu > li').click(function() {
var cnt = $(this).children().html();
$('input[type="hidden"]').val(cnt);
$('.number').html(cnt);
});
});
</script>
<hr />
@foreach($tweets as $tweet)
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="well">
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="{{ $tweet->user->profile_image_url }}">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{ $tweet->user->name }}</h4>
{{ $tweet->text }}
</div>
<small class="pull-right">{{ Twitter::ago($tweet->created_at) }}</small>
</div>
</div>
</div>
</div>
@endforeach
<div class="row">
<div class="text-center">
{!! $tweets->render() !!}
</div>
</div>