laravel search multiple words separated by space

前端 未结 7 1920
鱼传尺愫
鱼传尺愫 2020-12-29 08:57

I am new to laravel query builder, I want to search multiple words entered in an input field for example if I type \"jhon doe\" I want to get any column that contains jhon o

相关标签:
7条回答
  • 2020-12-29 09:52

    You can try as follows

    $keywordRaw = "jhon doe";
    $bindArr = explode(" ", $keywordRaw);
    
    $query = "select * from users where";    
    foreach ($i = 0; $i < count($bindArr); $i++) {
        if ($i == 0) {
            $query.= ' first_name LIKE "%?%"';
        } else {        
            $query.= ' or first_name LIKE "%?%"';
        }
    }
    
    $sth = $dbh->prepare($query);
    $sth->execute($bindArr);
    
    0 讨论(0)
提交回复
热议问题