['views', 'desc'], 2 => ['views', 'asc'], 3 => ['price', 'desc'], 4 => ['price', 'asc'], ]; public function index(): array { //this db comes from main Controller as included DI to construct $posts = $this->db->table('posts'); foreach ($this->ids as $key => $id) { if (is_numeric(request($key))) { $posts->where($id, '=', request($key)); } } //predictive search with insensitive to upper & lower cases if (request('q')) { $posts->where('title', 'like', '%' . request('q') . '%'); } //numeric because bool in db stores as tinyint, you can specify and change them with model's cast attribute foreach ($this->bools as $bool) { if (is_numeric(request($bool)) === true) { $posts->where($bool, '=', true); } } if (is_numeric(request('from'))) { $posts->where('price', '>', request('from')); } if (is_numeric(request('to'))) { $posts->where('price', '<', request('to')); } foreach ($this->sorts as $key => $sort) { if (request('sort') === $key) { $posts->orderBy($sort[0], $sort[1]); } } //return as array with pagination return $posts->paginate()->toArray(); } }