Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use whereLike for case-insensitive search #2924

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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