Slide 1

Slide 1 text

QueryFilter: Model Filtering Junior Grossi @ Darkmira Tour PHP 2018 April 2018

Slide 2

Slide 2 text

About me @junior_grossi

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Contact GitHub: http://github.com/jgrossi Twitter: http://twitter.com/junior_grossi Blog: https://blog.jgrossi.com

Slide 5

Slide 5 text

Talk's dynamics 35' talk + 5' questions = 40' 20% slides + 80% live coding

Slide 6

Slide 6 text

What's QueryFilter ? Code abstraction for filtering objects based on the `URL. From Laracasts: http://laracasts.com

Slide 7

Slide 7 text

/posts?title=foo&status=bar Select Post which title contains foo ; Select Post which status is bar .

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

composer require jgrossi/corcel

Slide 10

Slide 10 text

Listing posts in JSON public function index() { $posts = Post::newest()‑>limit(10)‑>get(); return PostResource::collection($posts); // json }

Slide 11

Slide 11 text

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); }

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

Show me the code Talk is cheap. Show me the code. ‑‑ Linus Trovals

Slide 14

Slide 14 text

After 20 minutes of live coding...

Slide 15

Slide 15 text

QueryFilter is a concept

Slide 16

Slide 16 text

Update: Talk's code https://github.com/jgrossi/darkmira‑query‑filter

Slide 17

Slide 17 text

Composer Packages cerbero/query‑filters kblais/query‑filter

Slide 18

Slide 18 text

Thanks! Questions? GitHub: http://github.com/jgrossi Twitter: http://twitter.com/junior_grossi Blog: https://blog.jgrossi.com