PHPackages                             media24si/utilities - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. media24si/utilities

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

media24si/utilities
===================

Media24 Laravel utilities

v2.0.5(2y ago)0693MITPHPPHP ^7.3|^8.0CI failing

Since Sep 4Pushed 2y ago2 watchersCompare

[ Source](https://github.com/Media24si/Utilities)[ Packagist](https://packagist.org/packages/media24si/utilities)[ RSS](/packages/media24si-utilities/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (9)Dependencies (4)Versions (11)Used By (0)

Media24 Laravel Utilities
=========================

[](#media24-laravel-utilities)

A collection of utilities we use in almost all projects.

[![Build Status](https://camo.githubusercontent.com/d8800d2ad8c2d2cf59ee8418a2a10f41ed1cb18925a36e6f6add13f9f3ae6979/68747470733a2f2f7472617669732d63692e6f72672f4d65646961323473692f5574696c69746965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Media24si/Utilities)

Compatibility
=============

[](#compatibility)

Use the 1.\* version for Laravel 5.\* / 6.\*

Installation
============

[](#installation)

```
composer require media24si/utilities

```

List of utilities
=================

[](#list-of-utilities)

1. Utilities
    1. [Api Paginator](#apipaginator)
    2. [Sorter](#sorter)
2. Scopes
    1. [WhereWhen](#wherewhen)
    2. [ApiPaginate](#apipaginate)
3. Rules
    1. [CsvIn](#csvin)

Utilities
=========

[](#utilities)

ApiPaginator
------------

[](#apipaginator)

An extension of the Laravel LengthAwarePaginator with a custom transformed response.

Example usage:

```
$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):

```
[
    '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
------

[](#sorter)

A Rule for validation and trait with a scope. Primarily used to enable easy sorting in API endpoints.

Example usage:

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 "-".

- /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
------

[](#scopes)

### WhereWhen

[](#wherewhen)

WhereWhen scope trait is an extension to the when method

```
$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', '
