Home >php教程 >php手册 >Laravel4 restful controller分页

Laravel4 restful controller分页

WBOY
WBOYOriginal
2016-06-06 20:09:531543browse

Laravel4 restful controller分页,使用下面方法分页报错:The Response content must be a string or object implementing __toString(), object given. class ApiController extends BaseController { protected $restful =true; public function getIndex(

Laravel4 restful controller分页,使用下面方法分页报错:The Response content must be a string or object implementing __toString(), “object” given.

class ApiController extends BaseController
{
protected $restful =true;
public function getIndex()
{
//博客首页
$p = Input::get(‘p’,1);
$blogs = Blog::with(‘author’)->orderBy(‘created_at’, ‘desc’)->paginate(20);
return $blogs;
}

}

今天被Laravel4 restful controller分页问题困扰了一天,今晚终于忍不住翻看源码,终于找到了解决方法:

class ApiController extends BaseController
{
protected $restful =true;
public function getIndex()
{
//博客首页
$p = Input::get(‘p’,1);
$blogs = Blog::with(‘author’)->orderBy(‘created_at’, ‘desc’)->select(‘id’, ‘title’)->forPage($p,2)->get();//paginate(20);
return $blogs;
}

}

现在分享出来希望对大家有帮助

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn