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:  open new tab react Example with Tutorial

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:  how to add calendar in html form?

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:  Class HTML FORM not found in Laravel 5 Error Solution

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:  Simple jQuery Fade Effects Example
  2. Read Also:  How to print or echo all the values of an array in PHP?
  3. Read Also:  Get URL Parameters using jQuery Example
  4. Read Also:  Get Current User ID from Mysql in Laravel
  5. Read Also:  Send PHP variable to bootstrap modal popup via Ajax
  6. Read Also:  Laravel 5.6 PDF Generation using Dompdf Example
  7. Read Also:  Bootstrap Toggle Inside JQuery DataTable
  8. Read Also:  Get URL Parameters using jQuery Example
  9. Read Also:  Simple jQuery Fade Effects Example
  10. Read Also:  Bootstrap Toggle Inside JQuery DataTable
CLOSEX