2017 © Pedro Peláez
 

library laravel-scoresearch

image

studio-net/laravel-scoresearch

  • Thursday, November 30, 2017
  • by cmizzi
  • Repository
  • 4 Watchers
  • 3 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 29 % Grown

The README.md

Laravel ScoreSearch package

Welcome home ! Let me introduce you this package. In fact, this package handles two importants things :, (*1)

  • grammar files (MySQL and PostgreSQL) ;
  • a simple trait ;

ScoreSearch allows you to search thought any of entity that implement the provided trait a new way based on scoring to fetch the more relevant rows. At now, I just tested the package over PostgreSQL. Maybe others database engines will be implemented if someone need them., (*2)

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads License Build Status, (*3)

Installation

The most basic installation is needed., (*4)

composer require 'studio-net/laravel-scoresearch @dev'

Usage

Add the provided trait into the wanted Eloquent model and provides a simple searchable protected variable to list the weight of each element. If a column is not specified, the default weight will be 1., (*5)

# app/User.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use StudioNet\ScoreSearch\Searchable;

/**
 * User
 *
 * @see Model
 */
class User extends Model {
    use Searchable;

    /** @var array $searchable */
    protected $searchable = [
        'columns' => [
            'username' => 10,
            'age'      => 5,
            'email'    => 15,
        ]
    ];
}

Now, you can simply return a search using the search scope, passing as first argument an array of comparaisons to use., (*6)

$users = User::search([
        'username' => ['or' => ['cyril', 'studio-net']],
        'email'    => ['or' => ['%@studio-net.fr', '%@studio-net.com'], '%boss'],
        'age'      => '(gt) 20',
    ])

    ->where('active', true)
    ->get();
select * from (
    select users.*, (
        (case when (((username = 'cyril') OR (username = 'studio-net'))) then 10 else 0 end) +
        (case when (((email ilike '%@studio-net.fr') OR (email ilike '%@studio-net.com')) AND (email ilike '%boss')) then 15 else 0 end) +
        (case when (age > 20) then 5 then 0 end)
    ) as score from users
) as users where score >= ? and active is true order by score desc

Specification

Operator PostgreSQL MySQL
lt < <
lte <= <=
gt > >
gte >= >=
% ilike like (but process a lower)
= = =

In order to use operator, you can refer to example below. Otherwise, this is the default syntax to use : (operator) text, excepting for % and = that doesn't need to handle an operator case, just the string within like cyril will produce = cyril and cyril% will produce ilike 'cyril%'., (*7)

Relationship

You can specify in your $searchable variable instance a joins array that can handle table names. You can specify the on by setting a subset., (*8)

    protected $searchable = [
        'joins'   => ['posts'],
        'joins'   => ['posts' => ['posts.id', 'users.id']],

        'columns' => [
            'posts.title' => 5,
            'user.login'  => 10
        ]
    ];

    // Fetch users with related posts
    User::search(['user.login' => 'cyril', 'posts.title' => '%toto%'])->with('posts')->get();

Eloquent

You can use every method that Eloquent provides. As this package use a subquery, you can easily make queries like :, (*9)

User::where('role', 'admin')
    ->search(['email' => '%@studio-net.fr'])
    ->with('posts')
    ->paginate(20);

Contributing

Thank you for considering contributing to this Laravel package. You are able to create issue and pull request in order to improve this powerful package., (*10)

License

This Laravel package is open-sourced software licensed and under the MIT license., (*11)

The Versions

30/11 2017