Skip to content

Commit

Permalink
Add search
Browse files Browse the repository at this point in the history
  • Loading branch information
thanh-taro committed Jun 9, 2016
1 parent bbdd7f0 commit 963da08
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ public function index(Request $request)

$this->validate($request, [
'page.size' => 'integer|min:0',
'page.number' => 'integer|min:1',
]);
$params = $request->only('page');
$pageSize = isset($params['page']['size']) ? intval($params['page']['size']) : 10;
$params = $request->only('page', 'q');
$pageSize = isset($params['page']['size']) ? intval($params['page']['size']) : 15;
$pageNumber = isset($params['page']['number']) ? intval($params['page']['number']) : 1;
$collections = call_user_func_array([$this->modelClass, 'paginate'], [$pageSize, ['*'], 'page[number]', $pageNumber]);

if (empty($params['q'])) {
$collections = call_user_func_array([$this->modelClass, 'paginate'], [$pageSize, ['*'], 'page[number]', $pageNumber]);
} else {
$builder = call_user_func([$this->modelClass, 'query']);
foreach ((new $this->modelClass())->getSearchable() as $column) {
$builder->orWhere($column, 'LIKE', "%{$params['q']}%");
}
$collections = $builder->paginate($pageSize, ['*'], 'page[number]', $pageNumber);
}

return JDR::generateData(Helper::makeJsonapiObject($this->jsonapiVersion, 'toplevel')->setPagination($collections));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Models/V1_0/ResourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public function getResourceType();
public function getResourceAttributes();

public function getResourceLinks();

public function getSearchable();
}

0 comments on commit 963da08

Please sign in to comment.