첫째, 불쌍한 나의 영어.
5 개의 기사로 가장 높은 조회수를보고 싶습니다. 시도했지만 Eloquent 쿼리에 문제가 있습니다.Laravel5 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
DB 쿼리 결과는
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)
나는이 주제에 대한 다른 게시물을 본 적이 있지만 나는 솔루션을하지 않았다.
도움 주셔서 감사합니다. 당신이 컨트롤러에서 서비스 sttuf에있는
@forelse($articles() as $article)
<img src="{{ $article->thumbnail }}" alt="{{ $article->title }}" title="{{ $article->title }}">
@forelse
<p class="text-center text-danger">
empty...
</p>
@endforelese
어쩌면 당신이 https://laravel.com/docs을 확인해야
그래서 컨트롤러에서 당신은 어떻게 설득력을 사용할 수 있습니다 /5.5/eloquent#retrieving- 싱글 - 모델 꽤 도움이 – BARNOWL
또한 코드는 깨끗하지 않습니다, 심각하게 laravel 워드 프로세서보세요. – BARNOWL
왜 두 개의 하위 쿼리가 필요합니까? 내부 쿼리는 사용중인 쿼리와 동일한 결과를 제공하지 않습니다. – rypskar