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