laravel使用DB模糊查询问题

文档有个示例:https://laravel.com/docs/5.3/...
如下:

$users = DB::table('users')
                ->where('name', 'like', 'T%')
                ->get();

问题1:
上面的示例只搜索一个字段name,如果我有一张articles表,有title和content两个字段需要模糊搜索,应该怎么写呢?

$keywords= $request->input('keywords');
$articles = DB::table('articles')
                ->where('content', 'like', $keywords.'%') //这一句里面不止content,而是title和content两个字段
                ->get();

问题2:
搜索中文还需要别的什么设置或操作吗?

阅读 14.2k
2 个回答
$keywords= $request->input('keywords');
$articles = DB::table('articles')
                ->where('content', 'like', $keywords.'%')
                ->orWhere('title', 'like', $keywords.'%')
                ->get();

使用Like搜索中文是不需要其它设置或操作的,对于这种全文搜索操作不建议使用like,最好是使用搜索引擎如ElasticSearch或者Sphinx等工具来实现

全文搜索可以试试

select * from articles where match (title,content) against ('keywords');