Another approach for this example is:
in PostController
public function index(Request $request) { $posts = Post::query() ->postFilter() ->get(); return view('demo', compact('posts')); }
in Post model
public function scopePostFilter($query) { $query->when( request('active'), fn ($query) => $query->where('active', request('active')) ); $query->when( request('sort'), fn ($query) => $query->orderBy('title', request('sort')); ); }
Your approach is specific for only post model. And above Pipeline example in tutorial can be used in multiple contollers.
you can make a trait and use it in each model you want
Nice abstractions.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another approach for this example is:
in PostController
in Post model
Your approach is specific for only post model. And above Pipeline example in tutorial can be used in multiple contollers.
you can make a trait and use it in each model you want
Nice abstractions.