Laravel 5 全文搜索

kis*_*san 8 php laravel laravel-5

我有一个包含 first_name 和 last_name 字段的数据库表。我需要检查 Asiapay 提供的信用卡持有人姓名是否是我旅行团的一部分(记录在我的数据库表中)。问题是给出的信用卡持有人是全名(单字段)

到目前为止,我的选择是:

我需要一些有关字符串比较的其他库和策略的建议

PS:我只是无法比较连接的名字和姓氏与holder_name,因为在某些情况下,旅行证件中的姓名与某人信用卡中的姓名不同(例如,护照中的“John Foo Doe”与信用卡中的“John Doe”

Mir*_*nan 13

以下是您应该如何实现全文搜索

首先在您的表格上创建全文搜索。

// Full Text Index
DB::statement('ALTER TABLE contacts ADD FULLTEXT fulltext_index (firstName, lastName, email)');
Run Code Online (Sandbox Code Playgroud)

然后在你的模型中

$columns = 'firstName,lastName,email';
    $contacts = Contact::where('customer_id', $request->user()->customer_id)
        ->select(['id', 'firstName', 'lastName', 'email', 'phone', 'mobile', 'lastContacted', 'assignedTo', 'createdBy'])
        ->whereRaw("MATCH ({$columns}) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($q))
        ->paginate(10);
Run Code Online (Sandbox Code Playgroud)

这是fullTextWildcards用全文搜索通配符替换空格的函数。

protected function fullTextWildcards($term)
{
    // removing symbols used by MySQL
    $reservedSymbols = ['-', '+', '<', '>', '@', '(', ')', '~'];
    $term = str_replace($reservedSymbols, '', $term);

    $words = explode(' ', $term);

    foreach ($words as $key => $word) {
        /*
         * applying + operator (required word) only big words
         * because smaller ones are not indexed by mysql
         */
        if (strlen($word) >= 3) {
            $words[$key] = '*' . $word . '*';
        }
    }

    $searchTerm = implode(' ', $words);

    return $searchTerm;
}
Run Code Online (Sandbox Code Playgroud)


del*_*8uk -1

您可以将字符串分解为一个空格:

$name = explode(' ', $name);
Run Code Online (Sandbox Code Playgroud)

然后获取名字:

$first = array_shift($name); 
Run Code Online (Sandbox Code Playgroud)

然后获取姓氏:

$last = array_pop($name);
Run Code Online (Sandbox Code Playgroud)

那么剩下的应该是相当微不足道的。祝你好运!