如何在laravel中执行此查询?

时间:2017-12-09 10:42:17

标签: mysql laravel

我想从laravel中的多个表中选择数据。如何在laravel中进行这样的查询? 这是我想在laravel中使用分页

的查询
$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);

1 个答案:

答案 0 :(得分:2)

这是一个非常标准的查询。首先,您需要为每个表创建一个模型,并在BlogPost模型中创建define relationships

要计算关系,请使用withCount()方法。