在 laravel 中出现数组错误时调用成员函数 paginate()
Call to a member function paginate() on array error in laravel
我想在 laravel 中创建分页,但出现错误。
这是错误 "Call to a member function paginate() on array"
这是我的查询
$blog = DB::select("select blog_post.*,(select img_name from blog_image WHERE blog_image.bid=blog_post.bid limit 0,1) as img_name,
(SELECT count(*) FROM likes WHERE likes.bid =blog_post.bid) AS likes,
(SELECT count(*) FROM comment WHERE comment.bid =blog_post.bid ) as comments ,
(SELECT count(*) FROM blog_share WHERE blog_share.bid =blog_post.bid ) as share
from blog_post WHERE status=1 AND is_draft=0 AND is_publish=1 AND is_delete=0")->paginate(2);
您首先需要使用 Laravel Eloquent 重写您的查询。例如:
$blog = DB::table('blog_post')->where([ 'status' => 1, 'is_draft' => 0, 'is_publish' => 1, 'is_delete' = 0 ])->paginate(2);
您不需要严格地将所有计数/内部查询放入一个查询中。您可以使用 Eloquent Relationships and Mutators 为您获取该信息。比如下面的Relationship来获取图片:
class BlogPost extends Model
{
public function image()
{
return $this->hasOne('BlogImage');
}
}
我想在 laravel 中创建分页,但出现错误。 这是错误 "Call to a member function paginate() on array"
这是我的查询
$blog = DB::select("select blog_post.*,(select img_name from blog_image WHERE blog_image.bid=blog_post.bid limit 0,1) as img_name,
(SELECT count(*) FROM likes WHERE likes.bid =blog_post.bid) AS likes,
(SELECT count(*) FROM comment WHERE comment.bid =blog_post.bid ) as comments ,
(SELECT count(*) FROM blog_share WHERE blog_share.bid =blog_post.bid ) as share
from blog_post WHERE status=1 AND is_draft=0 AND is_publish=1 AND is_delete=0")->paginate(2);
您首先需要使用 Laravel Eloquent 重写您的查询。例如:
$blog = DB::table('blog_post')->where([ 'status' => 1, 'is_draft' => 0, 'is_publish' => 1, 'is_delete' = 0 ])->paginate(2);
您不需要严格地将所有计数/内部查询放入一个查询中。您可以使用 Eloquent Relationships and Mutators 为您获取该信息。比如下面的Relationship来获取图片:
class BlogPost extends Model
{
public function image()
{
return $this->hasOne('BlogImage');
}
}