我有一个网站建设中,有数据库列‘性别’和‘大小’在mysql表中。它们是用m,f表示性别,用s,m,l,xl表示大小的枚举。在刀片式服务器中显示这些数据时,我使用了数组来获得正确的视图。
现在我正在实现一个搜索功能,它的工作正常。当我输入'm‘作为关键字时,所有的过滤器都会起作用,搜索结果也会显示出来,但当我输入'male’时,问题就出现了,因为没有显示任何结果。
public static function apply(Builder $builder, $value, $checkbox)
{
//i am using a temporary fix like this. I need a proper fix for this.
if(strtolower($value) == "male"){
$value = "m";
} else if (strtolower($value) == "female") {
$value = "f";
} else if (strtolower($value) == "other") {
$value = "both";
}
if($checkbox == null) {
$a = $builder->whereHas ('product', function ($a) use ($value) {
$a->where('gender', 'LIKE', '%' . $value . '%');
});
} else {
$a = $builder->whereHas ('product', function ($a) use ($value) {
$a->where('gender', 'LIKE', '%' . $value . '%');
})->whereRaw('`stocks`.`quantity` < `stocks`.`low_stock_threshold`');
}
return $a;
}发布于 2018-10-30 15:12:10
假设您可以在您的sql脚本级别修复解决方案,下面是我的建议。
您可以首先规范化搜索文本以匹配缩写文本,然后在where条件中应用该规范化文本。
下面是你可以这样做的一种方法,
DECLARE @GenderSearchText NVARCHAR(50) = 'female'
DECLARE @NormalizedSearchText NVARCHAR(50)
SET @NormalizedSearchText =
CASE
WHEN @GenderSearchText = 'M' OR @GenderSearchText = 'Male'
THEN 'M'
WHEN @GenderSearchText = 'F' OR @GenderSearchText = 'Female'
THEN 'F'
ELSE @GenderSearchText
END
SELECT @NormalizedSearchText
--<<APPLY THIS @NormalizedSearchText to your actual where condition>>发布于 2018-10-30 15:54:06
在配置文件中,将这些缩写组成的数组定义为数组的键,如下所示:
$array{‘m’=>‘男性’}
现在,对于搜索查询中的$value,使用这个数组- $array$value。在您的示例中,它将如下所示:
$a->其中(‘性别’,‘喜欢’,'%‘。$array$value。'%');
https://stackoverflow.com/questions/53058768
复制相似问题