0

I want to paginate a custom static function.

When using Eloquent goes like this People::paginate(5); and paginates the results.

I need to do the same for this static function People::getOwners();

Opal
  • 81,889
  • 28
  • 189
  • 210
Makis
  • 1,214
  • 3
  • 16
  • 40

2 Answers2

2

Just do your query and pagination in the function, like this:

public function getOwners() {
    return self::query()->paginate(5); 
}
Sh1d0w
  • 9,340
  • 3
  • 24
  • 35
  • Nice answer, unfortunately i cannot use it like this cause i'm using the same function without pagination! – Makis Jun 03 '15 at 06:17
1

Depending on what is going on inside getOwners(), you could turn it into a query scope. On your People model, add the function:

public function scopeGetOwners($query) {
    // $query->where(...);
    // $query->orderBy(...);
    // etc.

    $return $query;
}

Now, getOwners() is treated like any other query scope modifier (e.g. where, orderBy, etc.):

People::getOwners()->paginate(5);
patricus
  • 59,488
  • 15
  • 143
  • 145