Package Data | |
---|---|
Maintainer Username: | BostjanOb |
Maintainer Contact: | bostjan.oblak@salomon.si (Bostjan Oblak) |
Package Create Date: | 2017-09-01 |
Package Last Update: | 2024-03-20 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-24 03:01:44 |
Package Statistics | |
---|---|
Total Downloads: | 661 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 4 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A collection of utilities we use in almost all projects.
composer require media24si/utilities
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
]
]
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 "-".
// 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();
}
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', '<');
// 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'));
});
The apiPaginate scope trait can be used to call the ApiPaginator on a builder instance
$model->where('foo', 'bar')->orderBy('foo', 'desc')->apiPaginate(15);
$model->where('foo', 'bar')->orderBy('foo', 'desc');
$model = ApiPaginator::create($model, 15);
To use this scope in your model, simply include it as you would any other trait.
class MyModel extends \Illuminate\Database\Eloquent\Model
{
use \Media24si\Utilities\Scopes\ApiPaginate;
}
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.
$request->validate([
'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);
$request->validate([
'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);