Skip to content

Commit

Permalink
Refactor whereRaw calls to lowerLike function for column definition
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddmd committed Sep 23, 2023
1 parent 23d1ad6 commit 09a3b95
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
16 changes: 8 additions & 8 deletions app/Http/Controllers/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public function index(Request $request)
public static function filterAsset($filter)
{
return Asset::where(function ($query) use ($filter) {
$query->whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("description") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("mac_address") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("ip_address") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("manufacturer") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("sku") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("location") LIKE ?', [caseInsensitiveMatch($filter)])
->orWhereRaw('LOWER("fqdn") LIKE ?', [caseInsensitiveMatch($filter)]);
$query->whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("description"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("mac_address"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("ip_address"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("manufacturer"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("sku"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("location"), [caseInsensitiveMatch($filter)])
->orWhereRaw(lowerLike("fqdn"), [caseInsensitiveMatch($filter)]);
if (is_int($filter)) {
$query->orWhere("id", "=", $filter);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ControlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function index(Request $request)
{
$filter = $request->input("filter", "");
if (!empty($filter)) {
$controls = Control::whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])->
orWhereRaw('LOWER("description") LIKE ?', [caseInsensitiveMatch($filter)])->
$controls = Control::whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])->
orWhereRaw(lowerLike("description"), [caseInsensitiveMatch($filter)])->
paginate(5)->withQueryString();
}
else {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ThreatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function index(Request $request)
{
$filter = $request->input("filter", "");
if (!empty($filter)) {
$threats = Threat::whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])->
orWhereRaw('LOWER("description") LIKE ?', [caseInsensitiveMatch($filter)])->
$threats = Threat::whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])->
orWhereRaw(lowerLike("description"), [caseInsensitiveMatch($filter)])->
paginate(5)->withQueryString();
}
else {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function index(Request $request)
public static function filterUser($filter)
{
return User::where(function ($query) use ($filter) {
$query->whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])->
orWhereRaw('LOWER("email") LIKE ?', [caseInsensitiveMatch($filter)]);
$query->whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])->
orWhereRaw(lowerLike("email"), [caseInsensitiveMatch($filter)]);
if (is_int($filter)) {
$query->orWhere("id", "=", $filter);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/AssetThreatsControlsManage.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function render()
$filter = $this->threatSearchTerm;
$search = Threat::whereNotIn("id", $this->asset->threats()->pluck("threat_id")->toArray())->
where(function ($query) use ($filter) {
$query->whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])->
orWhereRaw('LOWER("description") LIKE ?', [caseInsensitiveMatch($filter)]);
$query->whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])->
orWhereRaw(lowerLike("description"), [caseInsensitiveMatch($filter)]);
})->get();
if ($search->count() > 0) {
$this->threatsSearch = $search;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/ThreatControlsManage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function render(): Factory|View|Application
$filter = $this->searchTerm;
$search = Control::whereNotIn("id", $controls->pluck("control_id")->toArray())->
where(function ($query) use ($filter) {
$query->whereRaw('LOWER("name") LIKE ?', [caseInsensitiveMatch($filter)])->
orWhereRaw('LOWER("description") LIKE ?', [caseInsensitiveMatch($filter)]);
$query->whereRaw(lowerLike("name"), [caseInsensitiveMatch($filter)])->
orWhereRaw(lowerLike("description"), [caseInsensitiveMatch($filter)]);
})->get();
if ($search->count() > 0) {
$this->controls_search = $search;
Expand Down
7 changes: 6 additions & 1 deletion app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

function caseInsensitiveMatch($filter): string
{
return sprintf("%%%s%%",trim(strtolower($filter)));
return sprintf("%%%s%%", trim(strtolower($filter)));
}

function lowerLike($column): string
{
return sprintf('LOWER("%s") LIKE ?', $column);
}

0 comments on commit 09a3b95

Please sign in to comment.