簡體   English   中英

雄辯的ORM:count()刪除select(…)

[英]Eloquent ORM: count() remove the select(…)

我在Laravel-4之外使用Eloquent ORM,並且正在構建自定義Paginator。

首先,我使用Fluent Query Builder構建查詢。 我想使用count()獲取查詢可以返回的結果count() ,然后使用take(x)skip(y)進行自定義分頁。 我需要在take()->skip()->get() count()之前做count() ,這樣我才不會超出頁面范圍。 問題是,當我在查詢上使用count()方法時,似乎刪除了我之前添加的所有select

我將問題隔離到以下簡單示例:

$query = DB::table('companies')
           ->join('countries','companies.country_id','=','countries.id')
           ->select(
               'companies.name as company_name',
               'countries.name as country_name'
           );

$nbPages = $query->count();
$results = $query->get();

//$results contains all fields of both tables 'companies' and 'countries'

如果我顛倒計數的順序並得到,它工作正常:

$results = $query->get();
$nbPages = $query->count();

//$results contains only 'company_name' and 'country_name'

問題:使用這樣的方法是否有更優雅的方法:

$tmp = clone $query;
$nbPages = $tmp->count();
$results = $query->get();

不幸的是,沒有。 在github上公開有關該問題的問題: https : //github.com/laravel/framework/pull/3416

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM