Remluben
Remluben

Reputation: 1673

Laravel 4 - How to fetch models with skip() / take() but no where clause

I am currently developing an API with Laravel 4 and ran into an issue when trying to filter a list of users returned. What confuses me is, that the L4 docs say

Note: All methods available on the query builder are also available when querying Eloquent models.

Now some code to clearify things:

    $since = Input::has('since') ? strtotime(Input::get('since')) : 0;
    $skip = (int)Input::get('skip');
    $take = (int)Input::get('take');

    if ($since) {
        $builder = User::where('updated_at', '>', date('Y-m-d H:i:s', $since));
    }
    else {
        $builder = new User();
    }

    if ($take > 0) {
        $builder->take($take);
    }
    if ($skip > 0 && $take > 0) {
        $builder->skip($skip);
    }

    return count($builder->get());

As you can see I am trying to fetch models based on different criteria. Whenever the URL parameter since is provided, I intended to return users modified since the given datetime.

The problem is, that the 'skip' and 'take' parameters do not work with no 'since' set. Assuming there are 3 users in the database, the API returns

Shouldn't it return 2 users in the first case ( 3 in table, first skipped )?

Upvotes: 0

Views: 829

Answers (1)

radmen
radmen

Reputation: 1624

When using new User You're creating new model. Try User::newQuery(). It should return query builder for User model.

Upvotes: 3

Related Questions