首页 > 解决方案 > 在查询中搜索没有 UTF-8 字母的 UTF-8 通道

问题描述

当 slug 包含 UTF8 字母时,获得非 utf8 结果的最佳方法是什么?(例如:当我输入slug“Zaidimai”时,从数据库中选择所有像“Žaidimai”和“Zaidimai”的地方)。

当前代码:

$results = DB::table('channels') -> where([['name', 'like', '%' . $slug . '%'], ['status', '=', 'OK']]) -> orWhere([['slug', 'like', '%' . $slug . '%'], ['status', '=', 'OK']]) -> limit(3) -> get();

它工作正常,但是当我尝试通过在搜索栏中输入“Zaidimai”来搜索频道时,它没有显示我想要的频道(Žaidimai)。

标签: phpmysqllaravelpostgresqllaravel-5

解决方案


安装 'unaccent' Postgres 扩展,然后将本机 SQL 与该unaccent功能一起使用。:slug是下面查询中的参数。

select <whatever you need>
  from channels
  where (unaccent("name") ilike '%'||unaccent(:slug)||'%' or unaccent(slug) ilike '%'||unaccent(:slug)||'%')
  and status = 'OK'
  limit 3;

推荐阅读