Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add PaginatesModels trait #15741

Closed
wants to merge 1 commit into from

Conversation

ryanwinchester
Copy link
Contributor

@ryanwinchester ryanwinchester commented Oct 4, 2016

This trait adds a super duper handy method that will give you behaviour from requests slightly similar to what something like league/fractal gives you without needing to install a separate package or create transformers.

Between this trait, and Eloquent Models' $casts and $hidden properties, starting a basic API with about as much control as some external packages give you, will be really quick.

To use it:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Pagination\PaginatesModels;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, PaginatesModels;
}
<?php

namespace App\Http\Controllers\Api;

use App\Product;

class ProductsController extends Controller
{
    public function index()
    {
        $products = $this->paginate(Product::class);

        return response()->json($products);
    }
}

Then you can go to your route and add some of these optional parameters to page and filter:
url

response

Thoughts?

@taylorotwell
Copy link
Member

I think this is a pretty cool idea, but would like to see it fleshed out more as a package first I think. 😄 Ping me if you turn it into a package so I can take a look.

@ryanwinchester
Copy link
Contributor Author

ryanwinchester commented Oct 4, 2016

@taylorotwell alrighty :)

@ryanwinchester
Copy link
Contributor Author

@taylorotwell I've been using this on quite a few projects in the last year https://github.com/ryanwinchester/laravel-paginates

and now more recently I've been using it alongside this, since I had started copy-pasta'ing my pagination stuff a lot, https://github.com/ryanwinchester/laravue-paginator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants