Laravel - 在非对象上调用成员函数 paginate()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27651433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:35:40  来源:igfitidea点击:

Laravel - Call to a member function paginate() on a non-object

phplaravelpagination

提问by Lovelock

I am making a search page in Laravel and cant understand how to use the paginate method for this usage.

我正在 Laravel 中制作一个搜索页面,但无法理解如何将分页方法用于此用途。

I have used it else where, but not like this.

我在其他地方使用过它,但不是这样。

So my route is:

所以我的路线是:

Route::get('search', function()
{
  $q = Input::get('srch-term');
  $searchTerms = explode(' ', $q);
  $query = DB::table('blogs');

  foreach($searchTerms as $term)
  {
    $query->where('blogtitle', 'LIKE', '%'. $term .'%')
        ->where('frontpage', '1')
        ->orderBy('id', 'desc');
  }

  $results = $query->get()->paginate(15);
  $countBuilds = count($results);
  return View::make('search', compact('results')); 
});

That gives me the error in the title. If i dont use the paginate method then it works fine.

这给了我标题中的错误。如果我不使用 paginate 方法,那么它工作正常。

Where am I going wrong?

我哪里错了?

P.S. As a side note, I know I shouldn't be doing my DB things in the route, still getting my head around how to use functions from controllers etc in routes but thats not for here.

PS 作为旁注,我知道我不应该在路线中做我的数据库事情,仍然在思考如何在路线中使用控制器等的功能,但这不是在这里。

回答by Arryangga Aliev Pratamaputra

Route::get('search', function()
{
  $q = Input::get('srch-term');
  $searchTerms = explode(' ', $q);
  $query = DB::table('blogs');

  foreach($searchTerms as $term)
  {
    $query = $query->where('blogtitle', 'LIKE', '%'. $term .'%')
        ->where('frontpage', '1')
        ->orderBy('id', 'desc');
  }

  //remove ->get()

  $results = $query->paginate(15);
  $countBuilds = count($results);
  return View::make('search', compact('results')); 
});