2017 © Pedro Peláez
 

library utilities

Media24 Laravel utilities

image

media24si/utilities

Media24 Laravel utilities

  • Friday, October 27, 2017
  • by BostjanOb
  • Repository
  • 3 Watchers
  • 0 Stars
  • 100 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 11 % Grown

The README.md

Media24 Laravel Utilities

A collection of utilities we use in almost all projects., (*1)

Build Status, (*2)

Compatibility

Use the 1.* version for Laravel 5.* / 6.*, (*3)

Installation

composer require media24si/utilities

List of utilities

  1. Utilities
    1. Api Paginator
    2. Sorter
  2. Scopes
    1. WhereWhen
    2. ApiPaginate
  3. Rules
    1. CsvIn

Utilities

ApiPaginator

An extension of the Laravel LengthAwarePaginator with a custom transformed response., (*4)

Example usage:, (*5)

$model->where('foo', 'bar')->orderBy('foo', 'desc');
$results = ApiPaginator::create($model, 15);

An example of the response (with toArray() called on the returned paginator object):, (*6)

[
    'data' => ['item3', 'item4'],
    'pagination' => [
        'total' => 6,
        'per_page' => 2,
        'current_page' => 2,
        'last_page' => 3,
        'next_page_url' => '/?page=3',
        'prev_page_url' => '/?page=1',
        'from' => 3,
        'to' => 4
    ]
]

Sorter

A Rule for validation and trait with a scope. Primarily used to enable easy sorting in API endpoints., (*7)

Example usage:, (*8)

Sorting is determined through the use of the ‘sort’ (for example) query string parameter. The value of this parameter is a comma-separated list of sort keys. The default sort direction is asc, but can optionally be changed to desc by prefixing any key with "-"., (*9)

  • /api/posts?sort=views
  • /api/posts?sort=-views
  • /api/posts?sort=comments,-views
// Model
class Post extends Model {
    use \Media24si\Utilities\Scopes\Sorter;
}


// Controller action
public function index(Request $request) {
    $request->validate(['sort' => new \Media24si\Utilities\Rules\Sorter(['views', 'comments'])]);

    $posts = \App\Post::sorter($request->input('sort', ''))->get();
}

Scopes

WhereWhen

WhereWhen scope trait is an extension to the when method, (*10)

$model->whereWhen('foo');
// same as
$model->when(request('foo'), function($query) {
  return $query->where('foo', request('foo'));
});

$model->whereWhen('foo', 'bar');
// same as
$model->when(request('bar'), function($query) {
  return $query->where('foo', request('bar'));
});

$model->whereWhen('foo', 'bar', '<');
// same as
$model->when(request('bar'), function($query) {
  return $query->where('foo', '<', request('bar'));
});

$model->whereWhen('foo', 'bar', null, new Request(['bar' => 'foo']));
// same as
$model->when($request->input('bar'), function($query) {
  return $query->where('foo', $request->input('bar'));
});

ApiPaginate

The apiPaginate scope trait can be used to call the ApiPaginator on a builder instance, (*11)

$model->where('foo', 'bar')->orderBy('foo', 'desc')->apiPaginate(15);
Same as previously
$model->where('foo', 'bar')->orderBy('foo', 'desc');
$model = ApiPaginator::create($model, 15);
Trait usage

To use this scope in your model, simply include it as you would any other trait., (*12)

class MyModel extends \Illuminate\Database\Eloquent\Model
{
    use \Media24si\Utilities\Scopes\ApiPaginate;
}

Rules

CsvIn

A rule for validating if a sent csv value string exists within a given set of values. All sent csv values have to exist inside the provided set of values., (*13)

Valid
  • /api/orders?source=android,ios
$request->validate([
  'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);
Invalid
  • /api/orders?source=android,ios,otherval
$request->validate([
  'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);

The Versions

27/10 2017

dev-master

9999999-dev

Media24 Laravel utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Stradovnik

27/10 2017

v1.0.1

1.0.1.0

Media24 Laravel utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adam Stradovnik

26/10 2017

dev-api-paginate

dev-api-paginate

Media24 Laravel utilities

  Sources   Download

MIT

The Requires

 

The Development Requires

04/09 2017

v1.0.0

1.0.0.0

Media24 Laravel utilities

  Sources   Download

MIT

The Requires

 

The Development Requires