【问题标题】:Call to a member function paginate() on array error in laravel在 laravel 中的数组错误上调用成员函数 paginate()
【发布时间】:2018-05-19 04:06:36
【问题描述】:

我想在 laravel 中创建分页,但它给出了错误。 这是错误“调用数组上的成员函数 paginate()”

这是我的查询

$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);

【问题讨论】:

  • 使用 Eloquent 编写查询。否则你不能使用paginate
  • 谢谢,但是当我在上面写查询时,我需要所有结果都只有一个查询。这在 laravel 中是可能的。如果是,那怎么办?
  • 是的,可以在 Laravel 中使用 eloquent 函数和 DB::raw() 的组合。
  • 你能给我举个例子吗

标签: laravel-5 laravel-query-builder


【解决方案1】:

您首先需要使用Laravel Eloquent 重写您的查询。例如:

$blog = DB::table('blog_post')->where([ 'status' => 1, 'is_draft' => 0, 'is_publish' => 1, 'is_delete' = 0 ])->paginate(2);

您不需要将所有计数/内部查询放入一个查询中。您可以使用 Eloquent Relationships 和 Mutators 为您获取该信息。比如下面的关系来获取图片:

class BlogPost extends Model
{

  public function image()
  {
    return $this->hasOne('BlogImage');
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    相关资源
    最近更新 更多