php 如何使用查询得到准确的搜索结果,包括搜索长度和数据库中的数据长度?

hwamh0ep  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(87)
Route::any ( '/search', function () {
    $q = Input::get ( 'q' );
    if($q != "" ){
        $user = Customer::where ( 'email', 'LIKE', '%' . $q . '%' )->orWhere ( 'remarks', 'LIKE', '%' . $q . '%' )->get ();
        if (count ( $user ) > 0)
            return view ('search')->withDetails ( $user )->withQuery ( $q );
        else
            return view ('search')->withMessage ( 'Oops' );
    };
    return view ('search')->withMessage ( 'Oops' );
} );

如何获得准确的搜索结果?例如,如果数据库由多个电话号码组成,则搜索结果将仅返回我的数据输入的结果(例如,当我搜索“1”,一个单一的数字,数据库应该显示“哎呀”,如果没有'1'被发现,如果'1'被发现。搜索结果不应包含“x1xxxxx”、“1x”、“xxxxx 1”等。目前我的搜索结果返回给我的各种数据由'1'。救命啊!谢谢大家!

elcex8rz

elcex8rz1#

它很简单,只要改变你的口才查询如下

Route::any ( '/search', function () {
    $q = Input::get ( 'q' );
    if($q != "" ){
        // Here, change this line
        $user = Customer::where (['email'=>$q])->orWhere (['remarks'=>$q])->get ();
        if (count ( $user ) > 0)
            return view ('search')->withDetails ( $user )->withQuery ( $q );
    };
    return view ('search')->withMessage ( 'Oops' );
} );

相关问题