full text search with multiple text input

172 Views Asked by At

I'm trying to do a full text search with two text input fields.

 public function searchmatch()
{
     $hex = Input::get('HEX');
     $rgb = Input::get('RGB')

        $products = DB::table('products')->whereRaw(
        "MATCH(HEX,RGB) AGAINST(? IN BOOLEAN MODE)", 
        array($hex,$rgb)
    )->get();


    return view('search')->with('products', $products);
}

However it does not work. I tried storing both inputs into an array and it did not work, it only works if i use one input. What is the best way around it? I'm using Laravel 5.0. I have looked for a solution across the site also but I have not found one.

My view form looks like this:

{!! Form::model(null, array('route' => array('match.search'))) !!}
<ul>
<li><div id="hex">HEX:{!! Form::text('HEX') !!}</div></li>
<li><div id="rgb">RGB:{!! Form::text('RGB') !!}</div></li>
<li><div id="picked"></div></li>
<li>{!! Form::submit('Find Match', array('id' => 'submitbtn')) !!}</li>
</ul>
{!! Form::close('Search') !!}

This is the route:

Route::post(
'matchsearch', 
 array(
 'as' => 'match.search', 
 'uses' => 'SearchController@searchmatch'
    )
 );

 class ProductsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->engine = 'MyISAM';
        $table->string('name');
        $table->string('brand');
        $table->string('pathtoimage');
        $table->string('price');
        $table->text('description');
        $table->string('HEX');
        $table->string('RGB');
        $table->string('colour');
        $table->string('link');


    });
            DB::statement('ALTER TABLE products ADD FULLTEXT search(name,   brand,HEX,RGB,colour)');
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('products', function($table) {
            $table->dropIndex('search');
        });
    Schema::drop('products');
}

}

ID:1
Name: Styletto
Brand: Lime Crime
Imagepath: img/2_lime-crime-lipstick-in-styletto.jpg Price:$18.00 Description: Bold, opaque & recklessly loaded with pigment. For lips that speak louder than words! HEX:#1B191B RGB:27,25,27 Colour:Black
Link:http://www.limecrime.com/unicorn-lipstick/

1

There are 1 best solutions below

4
On BEST ANSWER

UPDATED: Try

$products = DB::table('products')
    ->whereRaw('MATCH(HEX,RGB) AGAINST(? IN BOOLEAN MODE)', array("$hex $rgb")                  
    ->get();

"$hex $rgb" - no FTS operators means $hex OR $rgb
"+$hex +rgb" - means $hex AND $rgb

Here is a SQLFiddle demo

Further reading: