PHP code example of media24si / utilities

1. Go to this page and download the library: Download media24si/utilities library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

media24si / utilities example snippets


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

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

// 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();
}

$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'));
});

$model->where('foo', 'bar')->orderBy('foo', 'desc')->apiPaginate(15);

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

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

$request->validate([
  'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);

$request->validate([
  'source' => new Media24si\Utilities\Rules\CsvIn(['web', 'android', 'ios', 'winphone'])
]);