1. Go to this page and download the library: Download kaveraa/unaccent-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
Product::whereAnyLikeUnaccent(['name', 'commercial_name', 'code'], $request->search)
->where('active', true) // (name LIKE ... OR commercial_name LIKE ... OR code LIKE ...) AND active = 1
->paginate();
->whereLikeUnaccent('name', $term) // column
->whereLikeUnaccent('products.name', $term) // table.column (useful with joins)
->whereLikeUnaccent('data->address->city', $term) // key in a JSON column
->whereLikeUnaccent(DB::raw("CONCAT(first_name, ' ', last_name)"), $term) // SQL expression
use Illuminate\Validation\Rule;
use Kaveraa\UnaccentSearch\Mode;
public function index(Request $request)
{
$validated = $request->validate([
'q' => ['nullable', 'string', 'max:100'],
'mode' => ['nullable', Rule::enum(Mode::class)], // contains, starts_with, ends_with, exact
]);
return Product::query()
->whereAnyLikeUnaccent(['name', 'code'], $validated['q'] ?? null, $validated['mode'] ?? Mode::Contains)
->paginate();
}
use Kaveraa\UnaccentSearch\Normalizer;
$em->createQuery("SELECT p FROM App\Entity\Product p WHERE UNACCENT(p.name) LIKE :term ESCAPE '!'")
->setParameter('term', Normalizer::pattern($search))
->getResult();
use Kaveraa\UnaccentSearch\Doctrine\UnaccentFunction;
$config->addCustomStringFunction(UnaccentFunction::NAME, UnaccentFunction::class);
use Kaveraa\UnaccentSearch\Doctrine\SqliteMiddleware;
$config->setMiddlewares([new SqliteMiddleware()]);
use Kaveraa\UnaccentSearch\Mode;
Product::whereLikeUnaccent('name', 'cre', Mode::StartsWith)->get();
Product::whereLikeUnaccent('name', 'cre', 'starts_with')->get(); // same result
use Kaveraa\UnaccentSearch\Normalizer;
use Kaveraa\UnaccentSearch\Mode;
Normalizer::normalize('Crème Brûlée'); // 'creme brulee'
Normalizer::pattern('Élève'); // '%eleve%' (escaped LIKE pattern)
Normalizer::pattern('Élève', Mode::StartsWith); // 'eleve%'
Normalizer::pattern('100%'); // '%100!%%' (the % is searched as a normal character)
use Kaveraa\UnaccentSearch\SqlExpression;
$sql = SqlExpression::wrap('`name`', SqlExpression::MYSQL);
// REPLACE(REPLACE(... LOWER(CAST(`name` AS CHAR)) ..., 'à', 'a') ...)
$pdo->prepare("SELECT * FROM products WHERE {$sql} LIKE ? ESCAPE '!'")
->execute([Normalizer::pattern($search)]);
use Kaveraa\UnaccentSearch\SqliteFunction;
SqliteFunction::register($pdo); // PDO, Pdo\Sqlite or SQLite3
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.