이것은 분명하지만 Eloquent는 여기서 결과 집합을 반환하지 않지만 컬렉션을 반환합니다.
당신이 (Builder::get
가 Connection::select
를 호출하는 Builder::runSelect
를 호출하는 Builder::getFresh
를 호출) 소스 파고 경우, 당신은 의도는 단순히 다음 sortBy을 가지고 모음 (에 배치되는 결과를 반환하는 것입니다 있다고 찾을 수 있습니다 방법). 당신은 모든 항목을로드하지 않고 페이지 매김을 갖고 싶어
/**
* Run a select statement against the database.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return array
*/
public function select($query, $bindings = array(), $useReadPdo = true)
{
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
{
if ($me->pretending()) return array();
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
$statement->execute($me->prepareBindings($bindings));
//** this is a very basic form of fetching, it is limited to the PDO consts.
return $statement->fetchAll($me->getFetchMode());
});
}
, 다음 (아래 복제) 마르신의 솔루션 @ 사용할 필요가 : 그래서 그냥 명확하게하기 위해,
$posts = Post::leftJoin('comments','posts.id','=','comments.post_id')->
selectRaw('posts.*, count(comments.post_id) AS `count`')->
groupBy('posts.id')->
orderBy('count','DESC')->
skip(0)->take(20)->get();
Thansk,이 솔루션은하지 않습니다 데이터베이스에서 모든 레코드를 가져 와서 정렬하면 매우 초기 레코드에서 20 레코드 만 가져옵니다. – dulan
@dulan, 두 번째 코드에 대해 이야기하고 있다면 그렇습니다. 첫 번째 코드에서 모든 레코드는 데이터베이스에서 가져와 일부만 표시하므로 두 번째 솔루션을 선택해야합니다 –
skip()을 사용하여 주셔서 감사합니다 –