Upgrade to Pro — share decks privately, control downloads, hide ads and more …

QueryFilter: Model Filtering

QueryFilter: Model Filtering

Filtering gives you a bunch of options. Many options and conditionals make your controller bigger and bigger. Let's use the QueryFilter concept to map all query string parameters with our model layer, cleaning up your controller.

Talk presented on Darkmira Tour PHP 2018, in Brasília, DF Brazil,

Junior Grossi

April 14, 2018

More Decks by Junior Grossi

Other Decks in Technology


  1. Resume PHPMG group organizer Software Engineer at InterNACHI (PHP/Laravel) Creator

    of Corcel PHP Ph.D. in Information Science ‑ UFMG 2018 (in progress) Master's in Information Science ‑ UFMG 2015 Specialization in Web Developmenet ‑ PUCMG 2013 MBA in Business Management ‑ FGV 2009 Bachelor's in Computer Science ‑ PUCMG 2007 Zend Certified Engineer ‑ 2011
  2. What's QueryFilter ? Code abstraction for filtering objects based on

    the `URL. From Laracasts: http://laracasts.com
  3. Example Listing posts in JSON format Posts from WordPress http://blog.jgrossi.com

    Using Corcel PHP http://github.com/corcel/corcel Use WordPress backend with Laravel or any PHP app Laravel 5.6 app to handle all the logic
  4. Listing posts in JSON public function index() { $posts =

    Post::newest()‑>limit(10)‑>get(); return PostResource::collection($posts); // json }
  5. Filtering by Request public function index(Request $request) { $query =

    Post::query()‑>newest()‑>limit(10); if ($request‑>filled('title')) { $value = $request‑>get('title'); $query‑>where('post_title', 'like', "%$value%"); } if ($request‑>filled('status')) { $query‑>where('post_status', $request‑>get('status' } $posts = $query‑>get(); return PostResource::collection($posts); }
  6. Filtering by QueryFilter SOLID's Single responsibility principle ... a class

    should have only a single responsibility. Dependency/Method Injection ... is a technique whereby one object supplies the dependencies of another object.
  7. Show me the code Talk is cheap. Show me the

    code. ‑‑ Linus Trovals