Created
April 5, 2020 10:52
-
-
Save zawyelwin/1804820a16400828bcb1d231a3889567 to your computer and use it in GitHub Desktop.
Laravel Dynamic Filters from Laracasts Forum series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filters; | |
use App\User; | |
class ThreadFilters extends Filters | |
{ | |
/** | |
* Registered filters to operate upon. | |
* | |
* @var array | |
*/ | |
protected $filters = ['by', 'popular', 'unanswered']; | |
/** | |
* Filter the query by a given username. | |
* | |
* @param string $username | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
protected function by($username) | |
{ | |
$user = User::where('name', $username)->firstOrFail(); | |
return $this->builder->where('user_id', $user->id); | |
} | |
/** | |
* Filter the query according to most popular threads. | |
* | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
protected function popular() | |
{ | |
$this->builder->getQuery()->orders = []; | |
return $this->builder->orderBy('replies_count', 'desc'); | |
} | |
/** | |
* Filter the query according to those that are unanswered. | |
* | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
protected function unanswered() | |
{ | |
return $this->builder->where('replies_count', 0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filters; | |
use Illuminate\Http\Request; | |
abstract class Filters | |
{ | |
/** | |
* @var Request | |
*/ | |
protected $request; | |
/** | |
* The Eloquent builder. | |
* | |
* @var \Illuminate\Database\Eloquent\Builder | |
*/ | |
protected $builder; | |
/** | |
* Registered filters to operate upon. | |
* | |
* @var array | |
*/ | |
protected $filters = []; | |
/** | |
* Create a new ThreadFilters instance. | |
* | |
* @param Request $request | |
*/ | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* Apply the filters. | |
* | |
* @param \Illuminate\Database\Eloquent\Builder $builder | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function apply($builder) | |
{ | |
$this->builder = $builder; | |
foreach ($this->getFilters() as $filter => $value) { | |
if (method_exists($this, $filter)) { | |
$this->$filter($value); | |
} | |
} | |
return $this->builder; | |
} | |
/** | |
* Fetch all relevant filters from the request. | |
* | |
* @return array | |
*/ | |
public function getFilters() | |
{ | |
return array_filter($this->request->only($this->filters)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment