在Laravel控制器中,条件未过滤掉未完整匹配单词的记录。

huangapple go评论69阅读模式
英文:

Where condition didn't filter the records without complete the word in Laravel conroller

问题

我想要搜索带有作者名字的书。如果作者是F. U. Umesh,我传递F. U. Umesh,那么它可以正常工作。但是,当我不传递.,像这样:F U Umesh时,没有找到结果。控制器上的代码如下:

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
                ->where(function ($query) use ($request) {
                    if ($request->has('author')) {
                        $query->where('author', 'like', '%' . $request->author . '%');
                        $this->filter_query['author'] = $request->author;
                    }
                })
                ->orderBy('code', 'asc')
                ->get();

如果您对此有任何想法,请指导我。
我需要能够无.进行搜索,比如F U Umesh

英文:

I want to search the book with the Author's Name. if the author is F. U. Umesh I passed F. U. Umesh then it works fine. But, When I Pass without . like: F U Umesh. No, Result was found. code on Controller like this:

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
            ->where(function ($query) use ($request) {
                if ($request->has('author')) {
                    $query->where('author', 'like', '%'.$request->author.'%');
                    $this->filter_query['author'] = $request->author;
                }
            })
            ->orderBy('code','asc')
            ->get();

If you have an idea about that Please guide me.
I need to search without . like: F U Umesh.

答案1

得分: 1

尝试使用以下两段代码之一:

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
    ->where(function ($query) use ($request) {
        if ($request->has('author')) {
            $query->where('author', 'LIKE', str_replace(' ', '%', $request->author));
            $this->filter_query['author'] = $request->author;
        }
    })
    ->orderBy('code', 'asc')
    ->get();

或者使用这个:

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
    ->where(function ($query) use ($request) {
        if ($request->has('author')) {
            $query->whereRaw('author REGEXP ?', [str_replace(' ', '\\.? ', $request->author)]);
            $this->filter_query['author'] = $request->author;
        }
    })
    ->orderBy('code', 'asc')
    ->get();

请注意,这是两种不同的代码选项,您可以根据您的需求选择其中一种。

英文:

Try with these 2 codes

1.

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
            ->where(function ($query) use ($request) {
                if ($request->has('author')) {
                    $query->where('author', 'LIKE', str_replace(' ', '%', $request->author));
                    $this->filter_query['author'] = $request->author;
                }
            })
            ->orderBy('code','asc')
            ->get();

or use this

$data['books'] = BookMaster::select('id', 'code', 'title', 'image', 'categories', 'author', 'publisher', 'status')
                ->where(function ($query) use ($request) {
                    if ($request->has('author')) {
                        $query->->whereRaw('author REGEXP ?', [str_replace(' ', '\\.? ', $request->author)]);
                        $this->filter_query['author'] = $request->author;
                    }
                })
                ->orderBy('code','asc')
                ->get();

答案2

得分: 0

这是预期的行为。您需要向该SQL查询添加特定的WHERE子句。类似于

WHERE REPLACE(author, ".", "") LIKE %...%

但如果您想保留两种行为,可以添加一个orWhere可能会起作用。

if ($request->has('author')) {
    $query->where('author', 'like', '%'.$request->author.'%')
          ->orWhereRaw('REPLACE(author, ".", "") LIKE ? ', ['%'.$request->author.'%']);
          //->orWhere(DB::raw('REPLACE(author, ".", "")', 'like', "%$request->author%");
}
英文:

That is expected behavior. You'd need to add a specific WHERE clause to that SQL query. Something like

WHERE REPLACE(author, ".", "") LIKE %...%

But if you want to keep both behaviors, adding an orWhere could work.

if ($request->has('author')) {
    $query->where('author', 'like', '%'.$request->author.'%')
          ->orWhereRaw('REPLACE(author, ".", "") LIKE %?%', [$request->author]);
          //->orWhere(DB::raw('REPLACE(author, ".", "")', 'like', "%$request->author%");
}


</details>



huangapple
  • 本文由 发表于 2023年6月22日 10:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定