英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论