search

Home  >  Q&A  >  body text

Laravel uses DB fuzzy query problem

The document has an example: https://laravel.com/docs/5.3/...
As follows:

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

Question 1:
The above example only searches for one field name. If I have an articles table with two fields title and content that need fuzzy search, how should I write it?

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

Question 2:
Are there any other settings or operations required to search for Chinese?

PHPzPHPz2825 days ago830

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-16 16:51:26

    $keywords= $request->input('keywords');
    $articles = DB::table('articles')
                    ->where('content', 'like', $keywords.'%')
                    ->orWhere('title', 'like', $keywords.'%')
                    ->get();
    

    Using Like to search Chinese does not require other settings or operations. It is not recommended to use like for this kind of full-text search operation. It is best to use search engines such as ElasticSearch or Sphinx to achieve it

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 16:51:26

    You can try full text search

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

    reply
    0
  • Cancelreply