Skip to content

Commit

Permalink
Use whereLike for case-insensitive search (#2924)
Browse files Browse the repository at this point in the history
* Added usage new laravel method for `like`

Fixed pint

* Fixed code style

---------

Co-authored-by: tabuna <[email protected]>
  • Loading branch information
tabuna and tabuna authored Nov 23, 2024
1 parent 19dbc19 commit 8b846d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
5 changes: 4 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
},
"trim_array_spaces": true,
"single_trait_insert_per_statement": false,
"new_with_parentheses": false
"new_with_parentheses": false,
"php_unit_method_casing": {
"case": "camel_case"
}
}
}
38 changes: 30 additions & 8 deletions src/Platform/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Orchid\Platform\Http\Controllers;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -80,14 +82,34 @@ private function buildersItems(
});
}

$model = $model->where(function ($query) use ($name, $search, $searchColumns) {
$query->where($name, 'like', '%'.$search.'%');
if ($searchColumns !== null) {
foreach ($searchColumns as $column) {
$query->orWhere($column, 'like', '%'.$search.'%');
}
}
});
if (InstalledVersions::satisfies(new VersionParser, 'laravel/framework', '>11.17.0')) {
$model = $model->where(function ($query) use ($name, $search, $searchColumns) {
$value = '%'.$search.'%';

$query->whereLike($name, $value);

$query->when($searchColumns !== null, function ($query) use ($searchColumns, $value) {
foreach ($searchColumns as $column) {
$query->orWhereLike($column, $value);
}
});
});
} else {
/**
* @deprecated logic for older Laravel versions
*/
$model = $model->where(function ($query) use ($name, $search, $searchColumns) {
$value = '%'.$search.'%';

$query->where($name, 'like', $value);

$query->when($searchColumns !== null, function ($query) use ($searchColumns, $value) {
foreach ($searchColumns as $column) {
$query->orWhere($column, 'like', $value);
}
});
});
}

return $model
->limit($chunk)
Expand Down

0 comments on commit 8b846d9

Please sign in to comment.