欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Laravel4 restful controller分页

程序员文章站 2024-01-22 17:04:28
...

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;
}

}

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