Skip to content

Instantly share code, notes, and snippets.

@jgrossi
Last active July 14, 2017 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgrossi/a0e3a249a95263af21e4a0d667db4f5e to your computer and use it in GitHub Desktop.
Save jgrossi/a0e3a249a95263af21e4a0d667db4f5e to your computer and use it in GitHub Desktop.
Model Filtering according the URL
<?php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
abstract class QueryFilter
{
/**
* @var Request
*/
protected $request;
/**
* @var Builder
*/
protected $builder;
/**
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* @param Builder $builder
* @return Builder
*/
public function apply(Builder $builder)
{
$this->builder = $builder;
foreach ($this->request->all() as $filter => $value) {
if (method_exists($this, $filter) and !empty($value)) {
call_user_func_array([$this, $filter], [$value]);
}
}
return $this->builder;
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// ...
/**
* @param Builder $query
* @param QueryFilter $filter
* @return Builder
*/
public function scopeFilter(Builder $query, QueryFilter $filter)
{
return $filter->apply($query);
}
// ...
}
<?php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
class UserQueryFilter extends QueryFilter
{
/**
* @param $parameter
*/
public function registered($parameter)
{
list($from, $to) = explode(',', $parameter);
$this->builder->whereBetween('created_at', [$from, $to]);
}
/**
* @param array|string $terms
*/
public function skill($terms)
{
$this->filterByTaxonomy($terms);
}
/**
* @param array|string $terms
*/
public function topic($terms)
{
$this->filterByTaxonomy($terms);
}
/**
* @param string $gender
*/
public function gender($gender)
{
if (in_array($gender, ['female', 'male'])) {
$this->builder->where('gender', $gender);
}
}
/**
* @param array|string $terms
*/
protected function filterByTaxonomy($terms)
{
$terms = is_array($terms) ? $terms : [$terms];
$this->builder->whereHas('terms', function (Builder $query) use ($terms) {
$query->whereIn('users_terms.id', $terms);
});
}
}
<?php
namespace App\Http\Controllers;
use App\Filters\UserQueryFilter;
use App\Repositories\User\UserRepository;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
/**
* @var User
*/
protected $user;
/**
* @param $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* @param UserQueryFilter $filter
* @param UserRepository $repository
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(UserQueryFilter $filter, UserRepository $repository)
{
$users = $repository->getNetworkUsers(10, $filter); // Using repository and passing the $filter object
// OR
$users = $this->user->newQuery()->filter($filter)->get(); // Using it directly on the model
return view('users.index', compact('users'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment