Laravel paginate() on custom static functions

840 views Asked by At

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();

2

There are 2 answers

0
patricus On BEST ANSWER

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);
1
Sh1d0w On

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

public function getOwners() {
    return self::query()->paginate(5); 
}