简体   繁体   English

使用LIKE的Laravel查询不会返回任何结果

[英]Laravel query using LIKE does not return any result

Hi guys I'm currently stuck in this issue. 嗨,大家好,我目前正陷入这个问题 I'm trying to search something on DB and doesn't return any results. 我正在尝试在数据库上搜索某些内容,但未返回任何结果。 Below are my codes. 以下是我的代码。

Search string 1 = Boston Celtics 搜索字符串1 =波士顿凯尔特人队

Search string 2 = Boston Celtics Can't 搜索字符串2 =波士顿凯尔特人队无法

DB post_title = Boston Celtics Can't Let Bulls Loss Impact Difficult Stretch DB post_title =波士顿凯尔特人队不能让公牛的损失影响难以缓解

QUERY 查询

$data = $searchPost->where('post_title', 'like', '%'.$search.'%')
            ->orderBy('post_date', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get();

Search string 1 returns a result but search string 2 is not. 搜索字符串1返回结果,但搜索字符串2不返回结果。

Simple, because your searching different string. 很简单,因为您搜索的字符串不同。

Search String 2 ".. Can't .." use Straight Quotes . 搜索字符串2“ ..不能..”使用直引号

' '

DB post_title ".. Can't .." use Curly Quotes . DB post_title“ ..不能..”使用弯引号

' '

For reference: Straight and curly quotes 供参考: 直引号和弯引号

My first guess would be that an apostrophe might be to blame - in SQL Server the concatenation operator is "+" so you also might want to make sure that is right. 我的第一个猜测是可能要引起撇号-在SQL Server中,串联运算符为“ +”,因此您可能还想确保这是正确的。 Maybe try removing an apostrophe character with: 也许尝试使用以下命令删除撇号字符:

replace($search, '''', '') replace($ search,'''',))

Four single quotes in Microsoft SQL is an escape sequence for a single quote. Microsoft SQL中的四个单引号是一个单引号的转义序列。

Try the following code: 尝试以下代码:

1) You code might be breaking string because of ' in can't . 1)你的代码可能会打破,因为串'can't

$data = $searchPost->where('post_title', 'like', "%{$search}%")
            ->orderBy('post_date', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get();

Had the same problem before 以前有过同样的问题

try this 尝试这个

$data = $searchPost->where('post_title', 'like', '"%'.$search.'%"')
        ->orderBy('post_date', 'desc')
        ->offset($offset)
        ->limit($limit)
        ->get();

as you can see there is a " before the % at the start and another after % in the end. 如您所见,在%的开头有一个“,而在%的末尾有另一个。

If you see carefully you Can't in your search and your DB, the ' does not look the same 如果您仔细查看无法搜索和数据库,则'看起来会不同

Can't vs Can't 不能vs不能

When the ' is not the same, sure it can't be searched. 如果'不相同,请确保无法搜索。 Simple. 简单。

Had to use str_replace method to change apostrophe character 必须使用str_replace方法更改撇号字符

$search = str_replace('-', ' ', urldecode($request->input('search')));
$search = str_replace("'", "’", $search);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM