dimanche 1 janvier 2017

Search engine query impoving Laravel 5.2

Hello everyone and happy new year! So I implemented a simple search engine for my application, so the users can search for products and buy what they like. Up until now, the queries focus on only 2 columns; title and description.

This is my query:

Product::where('title', 'LIKE', "%{$query}%")->orWhere('description', 'LIKE', "%{$query}%")->get();

This is the products table which is being queried:

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('imagePath');
        $table->string('title');
        $table->text('description');
        $table->string('category',30);
        $table->integer('price');
        $table->integer('quantity');
        $table->integer('XS');
        $table->integer('S');
        $table->integer('M');
        $table->integer('L');
        $table->integer('XL');
        $table->integer('XXL');
    });

I can modify my query like this:

Product::where('title', 'LIKE', "%{$query}%")->
orWhere('description', 'LIKE', "%{$query}%")->
orWhere('category', 'LIKE', "%{$query}%")->
orWhere('price', 'LIKE', "%{$query}%")->
orWhere('quantity', 'LIKE', "%{$query}%")->
orWhere('XS', 'LIKE', "%{$query}%")->
orWhere('S', 'LIKE', "%{$query}%")->
orWhere('M', 'LIKE', "%{$query}%")->
orWhere('L', 'LIKE', "%{$query}%")->
orWhere('XL', 'LIKE', "%{$query}%")->
orWhere('XXL', 'LIKE', "%{$query}%")->get();

Now my concern lies on 2 things; How can I make the query distinguish whether a user is searching for a product with a name that contains xl in the name or a product eg a t-shirt which is xl size?

Also if a user hits the spacebar in search field (_), it returns all products in database. If a user hits the spacebar twice (__), it returns a message "there are no such products etc", like it should. Is there any way to prevent that?

Any help would be really appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire