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.

Step 1 : Migrating our table

Create Database table with Alter Table

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

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' => 'ArticleController@articleSearch'
    )
);

Create Laravel View Files


Example 2: Laravel full text search Example

Implementing Full-Text search in Laravel with MySQL Example

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 :

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.

Leave a Comment