Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将mySQL查询转换为Laravel 5.4查询生成器_Mysql_Laravel_Laravel Query Builder - Fatal编程技术网

如何将mySQL查询转换为Laravel 5.4查询生成器

如何将mySQL查询转换为Laravel 5.4查询生成器,mysql,laravel,laravel-query-builder,Mysql,Laravel,Laravel Query Builder,请有人帮助我将此SQL查询转换为Laravel 5.4查询生成器语法。我一直在寻找解决方案,并找到了一些,但并不完全如我所愿 我有三张桌子: 提要: -饲料标识 -用户id -提要标题 用户: -身份证 -用户名 声明: -注释\u id -饲料标识 -正文 -用户id 在返回的视图中,我希望看到提要标题、用户用户名以及每个提要的所有注释计数。就像你在facebook上看到的:显示每篇文章的评论数量。我真的需要这样做: 这是我在MySQL数据库中尝试过的SQL代码,它在MySQL数据库中工作正常

请有人帮助我将此SQL查询转换为Laravel 5.4查询生成器语法。我一直在寻找解决方案,并找到了一些,但并不完全如我所愿

我有三张桌子:

提要: -饲料标识 -用户id -提要标题

用户: -身份证 -用户名

声明: -注释\u id -饲料标识 -正文 -用户id

在返回的视图中,我希望看到提要标题、用户用户名以及每个提要的所有注释计数。就像你在facebook上看到的:显示每篇文章的评论数量。我真的需要这样做:

这是我在MySQL数据库中尝试过的SQL代码,它在MySQL数据库中工作正常,但在Laravel中尝试实现时返回了一个错误

select *, count(comments.comment_id) as comment_count 
from `feeds` 
inner join `users` on 
`feeds`.`user_id` = `users`.`id` 
inner join `comments` on 
`feeds`.`feed_id` = `comments`.`comment_feed_id` 
group by `comments`.`comment_feed_id`

假设您的模型名为“Feed”,则应该这样做:


你能发布你的模型关系吗?我只是想用SQL来实现这个。。。但是谈论关系;提要中的用户id是一个外键,它是用户中的主键(id)。feed_id是feed中的主键,是comments表中的外键(feed_id),谢谢,但是它返回一个错误,上面写着:“找不到类feed”。。。你知道是什么原因吗?我修复了它打印的错误,但是现在,它没有返回值,页面是空白的,没有详细信息。我忘了在末尾加上->get()!现在应该可以工作了。它返回以下错误:SQLSTATE[42000]:语法错误或访问冲突:1055“behiind.feed.feed\u id”不在分组依据中(SQL:select*,count(comments.comment\u id)作为
feeds
internal join
users
users
id
user\u id
internal join
comments
feed
id
comment\u id
comment\u-id
分组,按
comments
comment\u-feed\id我得到了它,没有源。源id。。请参阅上面更新的查询。这是正在工作的SQL,请帮助转换为Laravel查询生成器<代码>选择feed.feed\u类别、feed.title、users.username、feed.feed\u创建位置、feed.feed\u视图、计数(comments.comment\u id)作为注释计数,从
comments
right join
feeds
on
feed\u id
commentscomment\u feed\u idleft join
users
id
feeds
user\u id
groupby
feeds
feedfeed,我会帮你的。但你为什么要取消接受?
Feed::selectRaw('feeds.*, count(comments.comment_id) as comment_count')
->join('users', 'users.id', '=', 'feeds.user_id')
->join('comments', 'feeds.feed_id', '=', 'comments.comment_feed_id')
->groupBy('comments.comment_feed_id')->get();
DB::table('feeds')
    ->selectRaw('*, count(comments.comment_id) as comment_count')
    ->join('users', 'users.id', '=', 'feeds.user_id')
    ->join('comments', 'feeds.id', '=', 'comments.comment_feed_id')
    ->groupBy('comments.comment_feed_id')
    ->get();