Home » PHP Laravel Full Text Search in MYSQL Database

PHP Laravel Full Text Search in MYSQL Database

Today, We want to share with you PHP Laravel Full Text Search in MYSQL Database.In this post we will show you Laravel 5.7 MySQL Full-text Searching, hear for Full Text Search in Laravel 5.7 Example we will give you demo and example for implement.In this post, we will learn about Full text search for multiple words using Laravel and MySQL with an example.

PHP Laravel Full Text Search in MYSQL Database

There are the Following The simple About PHP Laravel Full Text Search in MYSQL Database Full Information With Example and source code.

As I will cover this Post with live Working example to develop Implementing Full-Text search in Laravel, so the laravel 5.7 eloquent full text search for this example is following below.

Read Also:  Jquery Ajax Form Submit examples using PHP

Step 1 : Migrating our table

Create Database table with Alter Table

Simple example for Full text search for multiple words using Laravel and MySQL

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateArticlesTable extends Migration {

	public function up()
	{
		Schema::create('articles', function(Blueprint $table) {
			$table->engine = 'MyISAM'; // means you never can't use any types of the foreign key constraints
			$table->increments('id');
			$table->string('title');
			$table->text('content');
			$table->timestamps();
		});

		DB::statement('ALTER TABLE articles ADD FULLTEXT search(title, content)');
	}

	public function down()
	{
		Schema::table('articles', function($table) {
	    	$table->dropIndex('search');
		});
		Schema::drop('articles');
	}

}

Step 2 : Adding a Live search function to Laravel controller

Create Simple Laravel Controller

public function articleSearch()
{
	$q = Input::get('searchterm');
	$articles = $this->post->whereRaw(
		"MATCH(title,content) AGAINST(? IN BOOLEAN MODE)", 
		array($q)
	)->get();
	return View::make('articles.index', compact('articles'));
}

For Testing purpose on MYSQL query

SELECT * FROM `articles` WHERE MATCH(title,content) AGAINST('laravel' IN BOOLEAN MODE);

Step 3 :Define Laravel the route and modifying the balde view file

Define Laravel Route

Route::post(
    'articles/search', 
    array(
        'as' => 'articles.search', 
        'uses' => '[email protected]'
    )
);

Create Laravel View Files
<div class="search">
	{{ Form::model(null, array('route' => array('articles.search'))) }}
	{{ Form::text('searchterm', null, array( 'placeholder' => 'Articles Search query...' )) }}
	{{ Form::submit('Search') }}
	{{ Form::close() }}
</div>

Example 2: Laravel full text search Example

Implementing Full-Text search in Laravel with MySQL Example

Read Also:  CodeIgniter Create Custom Helper Example Tutorial

Laravel run this all in one query

$termquerys = explode(' ', $termquerys);
$query = People::query();

foreach($termquerys as $termquery){
    $query->where(function($q) use ($termquery){
        $q->where('membername', 'like', '%'.$termquery.'%')
        ->orWhere('memberlastname', 'like', '%'.$termquery.'%')
        ->orWhere('address', 'like', '%'.$termquery.'%')
        // and so on......
    });
}

$results = $query->get();

related models

foreach($termquerys as $termquery){
    $query->where(function($q) use ($termquery){
        $q->where('membername', 'like', '%'.$termquery.'%')
        ->orWhereHas('relationName', function($yourrelation) use ($termquery){
            $yourrelation->where('relation_attribute', 'like', '%'.$termquery.'%');
        });
    });
}

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Create JSON with json_encode and json_decode using PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Laravel Full Text Search in MYSQL Database.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Create Bootable USB Drive Steps - Bootable Pen Drive
  2. Read Also:  Send HTML Form Data as JSON via Ajax
  3. Read Also:  Angular 4 Custom Alert, Prompt, And Confirm Box
  4. Read Also:  Bind DropdownList with Ng-Repeat using Angular Example
  5. Read Also:  how to create forgot password Recovery (Reset) in php and MySQL?
  6. Read Also:  Could not fully remove the plugin in Wordpress
  7. Read Also:  global variable for all controller and views in Laravel
  8. Read Also:  Angular 4 Custom Alert, Prompt, And Confirm Box
  9. Read Also:  how to create forgot password Recovery (Reset) in php and MySQL?
  10. Read Also:  global variable for all controller and views in Laravel

Leave a Comment

Your email address will not be published. Required fields are marked *