[英]Laravel 4 Pagination not working (ErrorException)
I've searched for all solutions to implement pagination successfully, but nothing I've found worked. 我已经搜索了所有成功实现分页的解决方案,但是没有发现任何效果。 I'm new to Laravel so I have maybe only one of those basic misunderstandings.
我是Laravel的新手,所以我可能只有这些基本的误解之一。
I have created it like it is described in the current documentation. 我已经创建了它,如当前文档中所述。 And I get the first page successfully, exactly the content i need, but when i try to use the pagination, clicking on the "next"-arrow, I get an error message:
而且我成功获得了第一页,正是我需要的内容,但是当我尝试使用分页时,单击“下一个”箭头,我收到一条错误消息:
ErrorException
Undefined variable: reviews (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
I know what this generally means but absolutely no idea how to solve this. 我知道这通常意味着什么,但绝对不知道如何解决。
This is the form in the view file: 这是视图文件中的表单:
{{ Form::open(array('url' => '/search', 'class' => 'search', 'method' =>'GET')) }}
{{ Form::text('title') }}
{{ Form::submit('Suche')}}
{{ Form::close() }}
Here is my routes configuration: 这是我的路线配置:
Route::any('/search', array('uses' => 'HomeController@search'));
Here is my function in the HomeController: 这是我在HomeController中的功能:
public function search() {
$input = Input::get('title');
if($input && $input != ''){
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
foreach ($games as $game) {
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
}
return View::make('search', compact('reviews', 'input', 'games'));
}
Here is the view-code of the search.blade.php view, which displays the results: 这是search.blade.php视图的视图代码,显示结果:
@section('content')
<h2>Suchergebnisse</h2>
@foreach ($reviews as $review)
@foreach ($games as $game)
@if ($game->id == $review->game_id)
<h3> {{ $game->name }} </h3>
@endif
@endforeach
<p>{{ (preg_replace('/\n/i', '<br/>', $review->body)) }}</p>
@endforeach
{{ $reviews->appends(array('term' => $input))->links() }}
@stop
I thank you beforehand if someone knows why Laravel has a problem with this solution. 如果有人知道为什么Laravel对此解决方案有问题,我先谢谢您。
Edit: I've changed the compact-statement and replaced it with an simple array, just like that: 编辑:我已经更改了紧凑型语句,并用一个简单的数组替换了它,就像这样:
return View::make('search', array('reviews' => $reviews, 'input' => $input, 'games' => $games));
Then I've added global $games; global $reviews;
然后,我添加了
global $games; global $reviews;
global $games; global $reviews;
to the Controller, so the Error "Undefined variable: reviews" disappeared. 到控制器,因此错误“未定义的变量:评论”消失了。 Now I've got the following message, but only on page 2, page 1 works fine:
现在,我收到以下消息,但仅在第2页,第1页上可以正常工作:
ErrorException
Invalid argument supplied for foreach() (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
First of all you are doing this 首先,您正在执行此操作
foreach ($games as $game) {
// $reviews is being updated every time in loop
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, every time there is a new value is being set to $review
but it should be an array and that's why you should declare an empty array before the loop like this 因此,每次有一个新值设置为
$review
但它应该是一个数组,这就是为什么您应该在像这样的循环之前声明一个空数组
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, you'll get an array in the $review
variable. 因此,您将在
$review
变量中得到一个数组。
Regarding your error message Undefined variable: reviews
, it's not defined and it could be because if($input && $input != '')
condition doesn't match or $games
got nothing, so, try to debug step by step your code, at first make sure that execution control is being entered inside if
by making dd($input)
statement inside if
and then if it outputs anything then make sure $games
is not null/false by making a dd($games)
statement after the games query. 关于您的错误消息
Undefined variable: reviews
, Undefined variable: reviews
,可能是因为if($input && $input != '')
条件不匹配或$games
什么都没有,所以,请尝试逐步调试代码,首先,通过在if
dd($input)
语句,然后在输出任何内容的情况下,确保在if
dd($input)
了执行控制,然后通过在命令之后执行dd($games)
语句,确保$games
不为null / false。游戏查询。
You can change the code snippet to this 您可以将代码段更改为此
$input = Input::get('title');
if($input) {
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
if($games) {
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
return View::make('search', compact('reviews', 'input', 'games'));
}
}
// return a 404 (not found) or something like this or whatever, when nothing found
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.