使用通配符 Sql 在 TextField 中搜索单词
Using Wildcard Sql for searching a word in a TextField
为了更清楚我有这个字段
Columntobesearch
aword1 bword1
aword2 bword2
aword3 bword4
现在我想做的是使用 sql 通配符进行搜索,所以我所做的就是这样
%searchbox%
我在搜索的两端都放置了通配符,但它搜索的只是字段中的第一个词
当我搜索 'aword' 时,所有字段都显示了,但是当我搜索 'bword' 时,没有显示任何内容,请帮忙。
这是我的完整代码
$Input=Input::all();
$makethis=Input::flash();
$soptions=Input::get('soptions');
$searchbox=Input::get('searchbox');
$items = Gamefarm::where('roost_hen', '=',Input::get('sex'))
->where($soptions, 'LIKE','%' . $searchbox . '%')
->paginate(12);
如果您使用 mysql,您可以试试这个:
<?php
$q = Input::get('searchbox');
$results = DB::table('table')
->whereRaw("MATCH(columntobesearch) AGAINST(? IN BOOLEAN MODE)",
array($q)
)->get();
当然,您需要准备 table 以便在您的迁移文件中进行全文搜索
DB::statement('ALTER TABLE table ADD FULLTEXT search(columntobesearch)');
无论如何,这不是执行 FTS 的更具可扩展性也不是更有效的方法。
对于可扩展且可靠的全文搜索,我强烈建议您查看此任务的 elasticsearch and implement any Laravel package
为了更清楚我有这个字段
Columntobesearch
aword1 bword1
aword2 bword2
aword3 bword4
现在我想做的是使用 sql 通配符进行搜索,所以我所做的就是这样
%searchbox%
我在搜索的两端都放置了通配符,但它搜索的只是字段中的第一个词
当我搜索 'aword' 时,所有字段都显示了,但是当我搜索 'bword' 时,没有显示任何内容,请帮忙。
这是我的完整代码
$Input=Input::all();
$makethis=Input::flash();
$soptions=Input::get('soptions');
$searchbox=Input::get('searchbox');
$items = Gamefarm::where('roost_hen', '=',Input::get('sex'))
->where($soptions, 'LIKE','%' . $searchbox . '%')
->paginate(12);
如果您使用 mysql,您可以试试这个:
<?php
$q = Input::get('searchbox');
$results = DB::table('table')
->whereRaw("MATCH(columntobesearch) AGAINST(? IN BOOLEAN MODE)",
array($q)
)->get();
当然,您需要准备 table 以便在您的迁移文件中进行全文搜索
DB::statement('ALTER TABLE table ADD FULLTEXT search(columntobesearch)');
无论如何,这不是执行 FTS 的更具可扩展性也不是更有效的方法。 对于可扩展且可靠的全文搜索,我强烈建议您查看此任务的 elasticsearch and implement any Laravel package