0

I am using Eloquent ORM outside of Laravel-4 and I am building a custom Paginator.

First, I build a query using Fluent Query Builder. I want to get the number of result the query could return using count() and then I do a custom pagination using take(x) and skip(y). I need to do the count() before the take()->skip()->get() so I dont fall outside of the page range. The problem is that when I use the count() method on the query, it seems to remove any select I added previously.

I isolated the problem to this simple example:

$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'

If i invert the order of the count and get, it works fine:

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

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

Question: is there a more elegant way the using something like this:

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

1 Answer 1

1

There is not, unfortunately. Open issue on github about the problem: https://github.com/laravel/framework/pull/3416

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.