{"id":297710,"date":"2020-01-26T15:00:25","date_gmt":"2020-01-26T15:00:25","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297710"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297710","title":{"rendered":"QueryFilter: \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485520\/\">\u0425\u043e\u0447\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e URL-\u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0437\u044b\u043a PHP \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Laravel.<\/p>\n<h4>\u041a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h4>\n<p>  \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 <i>QueryFilter<\/i> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438.<\/p>\n<pre><code class=\"php\">GET \/posts?title=source&status=active<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c <i>\u043f\u043e\u0441\u0442\u044b<\/i> (\u043c\u043e\u0434\u0435\u043b\u044c Post) \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044f\u043c:<\/p>\n<ul>\n<li>\u041d\u0430\u043b\u0438\u0447\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u00absource\u00bb \u0432 \u043f\u043e\u043b\u0435 <i>title<\/i>;<\/li>\n<li>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00abpublish\u00bb \u0432 \u043f\u043e\u043b\u0435 <i>status<\/i>;<\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>  \u041c\u043e\u0434\u0435\u043b\u044c <i>Post<\/i><\/p>\n<pre><code class=\"php\"> &lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model;  class Post extends Model {     \/**      * The attributes that are mass assignable.      *      * @var array      *\/     protected $fillable = [         'name',         'id',         'title',         'slug',         'status',         'type',         'published_at',         'updated_at',     ]; }  <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442:<\/p>\n<pre><code class=\"php\">Route::get('\/posts', 'PostController@index');<\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b <i>Resource\\Post<\/i> \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 <i>JSON<\/i>:<\/p>\n<pre><code class=\"php\"> namespace App\\Http\\Resources;  use Illuminate\\Http\\Resources\\Json\\JsonResource;  class Post extends JsonResource {     \/**      * Transform the resource into an array.      *      * @param  \\Illuminate\\Http\\Request  $request      * @return array      *\/     public function toArray($request)     {         return [             'id' =&gt; $this-&gt;ID,             'title' =&gt; $this-&gt;post_title,             'slug' =&gt; $this-&gt;post_name,             'status' =&gt; $this-&gt;post_status,             'type' =&gt; $this-&gt;post_type,             'published_at' =&gt; $this-&gt;post_date,             'updated_at' =&gt; $this-&gt;post_modified,         ];     } } <\/code><\/pre>\n<p>  \u0418 \u0441\u0430\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441 \u043e\u0434\u043d\u0438\u043c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435\u043c:<\/p>\n<pre><code class=\"php\"> namespace App\\Http\\Controllers;  use App\\Http\\Resources\\Post as PostResource; use App\\Post;  class PostController extends Controller {     \/**      * @return \\Illuminate\\Http\\Resources\\Json\\AnonymousResourceCollection      *\/     public function index()     {         $posts = Post::limit(10)-&gt;get();          return PostResource::collection($posts);     } } <\/code><\/pre>\n<p>  \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c:<\/p>\n<pre><code class=\"php\"> \/**  * @param Request $request  * @return \\Illuminate\\Http\\Resources\\Json\\AnonymousResourceCollection  *\/ public function index(Request $request) {     $query = Post::limit(10);      if ($request-&gt;filled('status')) {         $query-&gt;where('post_status', $request-&gt;get('status'));     }      if ($request-&gt;filled('title')) {         $title = $request-&gt;get('title');         $query-&gt;where('post_title', 'like', &quot;%$title%&quot;);     }     $posts = $query-&gt;get();      return PostResource::collection($posts); } <\/code><\/pre>\n<p>  \u0421 \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u0440\u0430\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u0447\u0442\u043e \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e. <\/p>\n<h4>\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 QueryFilter<\/h4>\n<p>  \u0421\u043c\u044b\u0441\u043b \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434\u044b \u0441 \u043a\u0430\u0436\u0434\u044b\u043c \u043f\u043e\u043b\u0435\u043c, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f.<\/p>\n<p>  \u0424\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443:<\/p>\n<pre><code class=\"php\">GET \/posts?title=source&status=publish<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 <i>PostFilter<\/i> \u0438 \u043c\u0435\u0442\u043e\u0434\u044b <i>title()<\/i> \u0438 <i>status()<\/i>. <i>PostFilter<\/i> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <i>QueryFiler<\/i> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438.<\/p>\n<p>  <b>\u041c\u0435\u0442\u043e\u0434 <i>apply()<\/i><\/b><\/p>\n<p>  \u041a\u043b\u0430\u0441\u0441 <i>QueryFIlter<\/i> \u0438\u043c\u0435\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <i>apply()<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0432\u044b\u0437\u043e\u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432, \u0438\u043c\u0435\u044e\u0449\u0438\u0445\u0441\u044f \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435 <i>PostFilter<\/i>. <\/p>\n<pre><code class=\"php\"> namespace App\\Http\\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-&gt;request = $request;     }      \/**      * @param Builder $builder      *\/     public function apply(Builder $builder)     {         $this-&gt;builder = $builder;          foreach ($this-&gt;fields() as $field =&gt; $value) {             $method = camel_case($field);             if (method_exists($this, $method)) {                 call_user_func_array([$this, $method], (array)$value);             }         }     }      \/**      * @return array      *\/     protected function fields(): array     {         return array_filter(             array_map('trim', $this-&gt;request-&gt;all())         );     } } <\/code><\/pre>\n<p>  \u0421\u0443\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0447\u0435\u0440\u0435\u0437 <i>Request<\/i> \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 (\u043a\u043b\u0430\u0441\u0441 PostFilter). \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430.<\/p>\n<p>  <b>\u041a\u043b\u0430\u0441\u0441 <i>PostFilter<\/i><\/b><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043a\u043b\u0430\u0441\u0441\u0430 <i>PostFilter<\/i> \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0435\u0433\u043e <i>QueryFilter<\/i>. \u041a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c \u0440\u0430\u043d\u0435\u0435, \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <i>title($value)<\/i> \u0438 <i>status($value)<\/i><\/p>\n<pre><code class=\"php\"> namespace App\\Http\\Filters;  use Illuminate\\Database\\Eloquent\\Builder;  class PostFilter extends QueryFilter {     \/**      * @param string $status      *\/     public function status(string $status)     {         $this-&gt;builder-&gt;where('post_status', strtolower($status));     }      \/**      * @param string $title      *\/     public function title(string $title)     {         $words = array_filter(explode(' ', $title));          $this-&gt;builder-&gt;where(function (Builder $query) use ($words) {             foreach ($words as $word) {                 $query-&gt;where('post_title', 'like', &quot;%$word%&quot;);             }         });     } } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u044f \u043d\u0435 \u0432\u0438\u0436\u0443 \u0441\u043c\u044b\u0441\u043b\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0432\u043f\u043e\u043b\u043d\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b. \u0421\u043c\u044b\u0441\u043b \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0438\u043c\u0435\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0438 \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u0443\u044e \u0443\u0433\u043e\u0434\u043d\u043e \u043b\u043e\u0433\u0438\u043a\u0443 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  <b>\u0421\u043e\u0437\u0434\u0430\u0435\u043c <i>scopeFilter()<\/i><\/b><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/p>\n<pre><code class=\"php\"> \/**  * @param Builder $builder  * @param QueryFilter $filter  *\/ public function scopeFilter(Builder $builder, QueryFilter $filter) {     $filter-&gt;apply($builder); }  <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <i>filter()<\/i> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <i>QueryFilter<\/i>, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <i>PostFilter<\/i>.<\/p>\n<pre><code class=\"php\">$filteredPosts = Post::filter($postFilter)-&gt;get();<\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 <i>filter($postFilter)<\/i>, \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043e\u0442 \u043b\u0438\u0448\u043d\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438.<\/p>\n<p>  \u0414\u043b\u044f \u043e\u0431\u043b\u0435\u0433\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <i>scopeFilter<\/i> \u0432 \u0442\u0440\u0435\u0439\u0442 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<pre><code class=\"php\"> namespace App\\Http\\Filters;  use Illuminate\\Database\\Eloquent\\Builder;  trait Filterable {     \/**      * @param Builder $builder      * @param QueryFilter $filter      *\/     public function scopeFilter(Builder $builder, QueryFilter $filter)     {         $filter-&gt;apply($builder);     } } <\/code><\/pre>\n<p>  \u0412 <i>Post<\/i> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c:<\/p>\n<pre><code class=\"php\">  class Post extends CorcelPost {     use Filterable; <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <i>PostFilter<\/i> \u0432 \u043c\u0435\u0442\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 <i>index()<\/i> \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438 <i>filter()<\/i>.<\/p>\n<pre><code class=\"php\"> class PostController extends Controller {     \/**      * @param PostFilter $filter      * @return \\Illuminate\\Http\\Resources\\Json\\ResourceCollection      *\/     public function index(PostFilter $filter)     {         $posts = Post::filter($filter)-&gt;limit(10)-&gt;get();          return PostResource::collection($posts);     } } <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435. \u041c\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043b\u0438 \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0435\u0434\u0438\u043d\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 (S \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u043e\u0432 SOLID)<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>  \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438\u0446\u0435\u043f\u0430 \u0442\u043e\u043d\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 PHP \u0438 Laravel. \u041d\u043e \u043a\u0430\u043a \u044f \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u044d\u0442\u043e \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u0438 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c \u044f\u0437\u044b\u043a\u043e\u043c \u0438\u043b\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c. <\/p>\n<h4>\u0421\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/blog.jgrossi.com\/\">\u0411\u043b\u043e\u0433<\/a> Junior Grossi<\/li>\n<li><a href=\"https:\/\/packagist.org\/packages\/kblais\/query-filter\">\u0413\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0430\u043a\u0435\u0442<\/a> \u043e\u0442 Killian Blais<\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/485520\/\"> https:\/\/habr.com\/ru\/post\/485520\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485520\/\">\u0425\u043e\u0447\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e URL-\u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0437\u044b\u043a PHP \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Laravel.<\/p>\n<h4>\u041a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h4>\n<p>  \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 <i>QueryFilter<\/i> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438.<\/p>\n<pre><code class=\"php\">GET \/posts?title=source&#038;status=active<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c <i>\u043f\u043e\u0441\u0442\u044b<\/i> (\u043c\u043e\u0434\u0435\u043b\u044c Post) \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044f\u043c:<\/p>\n<ul>\n<li>\u041d\u0430\u043b\u0438\u0447\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u00absource\u00bb \u0432 \u043f\u043e\u043b\u0435 <i>title<\/i>;<\/li>\n<li>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00abpublish\u00bb \u0432 \u043f\u043e\u043b\u0435 <i>status<\/i>;<\/li>\n<\/ul>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-297710","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297710"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297710\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}