【发布时间】:2017-11-15 09:27:54
【问题描述】:
首先,对不起我的英语不好。
我想用 5 篇文章获得最高的浏览量。
我试过了,但是 Eloquent 查询有问题。
laravel 源码
<?php
$up = "SELECT id FROM articles WHERE id IN (SELECT id from (SELECT id FROM articles ORDER BY view_count DESC LIMIT 0,5) x)";
$builder = new \App\Article;
$query = $builder->selectRaw("articles.*, $up");
?>
@forelse($query->latest() as $article)
<img src="{{ $article->thumbnail }}" alt="{{ $article->title }}" title="{{ $article->title }}">
@empty
<p class="text-center text-danger">
empty...
</p>
@endforelse
数据库查询结果
MariaDB [test]> SELECT id FROM articles WHERE id IN (SELECT id from (SELECT id FROM articles ORDER BY view_count DESC LIMIT 0,5) x);
+------+
| id |
+------+
| 4018 |
| 4045 |
| 3800 |
| 4011 |
| 4005 |
+------+
5 rows in set (0.00 sec)
我已经看过有关此主题的其他帖子,但我没有得到解决方案。
感谢您的帮助。
【问题讨论】:
-
你的代码也不干净,请认真查看 laravel 文档。
-
为什么有两个子查询?内部查询的结果与您使用的查询不同吗?
标签: php mysql laravel relationship laravel-eloquent