2017 © Pedro Peláez
 

library laravel-paginates

Laravel controller trait for paginating models

image

ryanwinchester/laravel-paginates

Laravel controller trait for paginating models

  • Tuesday, November 7, 2017
  • by ryanwinchester
  • Repository
  • 1 Watchers
  • 2 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

PaginatesModels

Packagist Packagist, (*1)

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 all the setup and needing to create transformers., (*2)

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

Install

composer require ryanwinchester/laravel-paginates

Usage

Add it to your controller (or base controller, as shown):, (*4)

// ...
use RyanWinchester\Paginates\PaginatesModels;

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

Then use it in your controller methods like so:, (*5)

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

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

Sometimes because of security or privacy, you may want to limit include relationships and/or columns. If that is the case, then be sure to include the parameters you want, making sure to exclude any user-supplied include parameters., (*6)

$products = $this->paginate(
    Product::class,
    $request->except(['include', 'columns'])
);

Or, say you want to define some included relationships yourself:, (*7)

$products = $this->paginate(
    Product::with('variations'),
    $request->except(['include', 'columns'])
);

Or, even limit to specific columns:, (*8)

$products = $this->paginate(
    Product::select(['id', 'price', 'in_stock']),
    $request->except('columns')
);

You can pass in any builder instance or a model class name., (*9)

Parameters:

  • page : page=3 the page number
  • perPage : perPage=10 amount to show per page
  • columns : columns=title,body,author limit to certain columns
  • include : include=categories,tags load relations
  • orderBy : orderBy=published|desc order the items by a column and direction

In action

Then you can go to your route and add some of these optional parameters to page and filter: url, (*10)

response, (*11)

Please try it out and give feedback.

Taylor thinks it's a good idea, so I mean what other reason do you need?, (*12)

The Versions

07/11 2017

dev-master

9999999-dev

Laravel controller trait for paginating models

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

07/11 2017

v1.1.0

1.1.0.0

Laravel controller trait for paginating models

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

10/10 2017

v1.0.0

1.0.0.0

Laravel controller trait for paginating models

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

09/10 2017

v0.3.3

0.3.3.0

Laravel controller trait for paginating models

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

04/10 2016

v0.1

0.1.0.0

Laravel controller trait for paginating models

  Sources   Download

Apache-2.0

The Requires