SavefileArchive
USD/IDR ...
|
BTC ...
|
ETH ...
|
GOLD/gram ...
Terbaru
SavefileArchive — Tutorial coding, tips programming, dan dunia musik untuk developer & pecinta musik Indonesia

laravel make full text search on mysql

 

To implement full-text search on a MySQL database in Laravel, you can use the whereRaw and match methods on the Laravel query builder.

The whereRaw method allows you to specify a raw SQL expression as a where clause, and the match method allows you to specify a full-text search expression.

For example, to search for records that contain the word "hello" in the title column of the posts table, you can use the following code:

$posts = DB::table('posts') ->whereRaw('MATCH(title) AGAINST(? IN BOOLEAN MODE)', ['hello']) ->get();

The MATCH and AGAINST keywords are used to specify the full-text search expression, and the IN BOOLEAN MODE option is used to enable the Boolean mode, which allows you to use the +, -, and * operators to fine-tune the search.

You can also use the ->match method instead of ->whereRaw, as follows:

$posts = DB::table('posts') ->match('title', 'hello') ->get();

The ->match method is a shortcut for the ->whereRaw and MATCH and AGAINST keywords, and it automatically adds the IN BOOLEAN MODE option to the full-text search expression.