【问题标题】:Laravel 4 Pagination not working (ErrorException)Laravel 4 分页不起作用(ErrorException)
【发布时间】:2014-01-25 01:43:06
【问题描述】:

我已经搜索了所有成功实现分页的解决方案,但没有发现任何工作。我是 Laravel 的新手,所以我可能只有这些基本误解之一。

我按照当前文档中的描述创建了它。我成功获得了第一页,正是我需要的内容,但是当我尝试使用分页时,点击“下一页”-箭头,我收到一条错误消息:

ErrorException
Undefined variable: reviews (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)

我知道这通常意味着什么,但绝对不知道如何解决这个问题。

这是视图文件中的表格:

{{ Form::open(array('url' => '/search', 'class' => 'search', 'method' =>'GET')) }}
{{ Form::text('title') }}
{{ Form::submit('Suche')}}
{{ Form::close() }}

这是我的路线配置:

Route::any('/search', array('uses' => 'HomeController@search'));

这是我在 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'));
}

这里是 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

如果有人知道为什么 Laravel 对此解决方案有问题,我先谢谢你。

编辑: 我已经更改了紧凑语句并将其替换为一个简单的数组,就像这样:

return View::make('search', array('reviews' => $reviews, 'input' => $input, 'games' => $games));

然后我将global $games; global $reviews; 添加到控制器中,因此错误“未定义变量:评论”消失了。现在我收到以下消息,但仅在第 2 页,第 1 页可以正常工作:

ErrorException

Invalid argument supplied for foreach() (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)

【问题讨论】:

  • 您有什么理由使用compact 而只是创建一个数组?
  • 我刚刚用普通数组替换了“紧凑”,所以我有:return View::make('search', array('reviews' =&gt; $reviews, 'input' =&gt; $input, 'games' =&gt; $games)); 然后我将global $games; global $reviews; 添加到控制器中。现在错误发生了变化,它现在说:`ErrorException: Invalid argument provided for foreach() (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)``

标签: php laravel pagination


【解决方案1】:

首先你正在这样做

foreach ($games as $game) {
    // $reviews is being updated every time in loop
    $reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}

所以,每次有一个新值被设置为$review,但它应该是一个数组,这就是为什么你应该像这样在循环之前声明一个空数组

$reviews = []; // or array();
foreach ($games as $game) {
    $reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}

因此,您将在 $review 变量中获得一个数组。

关于您的错误消息Undefined variable: reviews,它没有定义,可能是因为if($input &amp;&amp; $input != '') 条件不匹配或$games 什么都没有,所以,尝试逐步调试您的代码,首先确保通过在if 中创建dd($input) 语句,在if 中输入执行控制,然后如果它输出任何内容,则通过在游戏查询后创建dd($games) 语句来确保$games 不为空/假。

你可以把代码sn-p改成这个

$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

【讨论】:

  • 我无法使用$reviews-&gt;links() 方法获取分页器。我收到以下错误,因为 $reviews 现在是非对象:Call to a member function links() on a non-object。我试图通过将 search.blade.php 文件中的它转换为一个对象来解决这个问题,比如$object = (object) $reviews,但是它说:Call to undefined method stdClass::links()
  • 因为$reviews 不是paginator 实例,它是paginator 实例的数组。第一个paginator 实例是$reviews[0]
  • 在您看来,每个$review(在foreach 循环内)都是paginator 的一个实例。
  • 好的,所以我把它放在了 foreach 循环中。现在我在查看分页的第二页时不再收到任何错误,但仍然是一个空页面:(我刚刚复制并粘贴了你的 sn-p,更改了搜索视图,其他代码就像以前一样
猜你喜欢
  • 2013-09-09
  • 2014-08-15
  • 1970-01-01
  • 2018-06-25
  • 2016-11-28
  • 2016-08-22
  • 1970-01-01
  • 2013-05-30
  • 2013-05-17
相关资源
最近更新 更多