在laravel5中构建搜索查询会出现错误语法错误或访问冲突:1064

时间:2016-04-19 08:44:09

标签: php laravel-5 mariadb

我想在laravel5 app中实现一个简单的搜索功能

use DB;
use App\Models\User;
use Illuminate\Http\Request;

use ConnectIn\Http\Requests;

class SearchController extends Controller
{
    public function getResults(Request $request)
    {

        $query = $request->input('name');
        if (!$query) {
            return  redirect()-> route('home') -> with('info', "Search must contain some charactres");
        }
        else{

        $users = User::where(DB::raw("CONCAT (first_name, ' ', last_name), "),
        "LIKE", "%{$query}%")
    ->orwhere('username', "LIKE", "%{$query}%")
    ->get();
    dd('search is ALL OK' . $users);
    }
        return view('search.results');
    }

}

使用上面提供的代码我收到错误

    QueryException in Connection.php line 673:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' LIKE ? or `username` LIKE ?' at line 1 (SQL: select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE %s% or `username` LIKE %s%)
    in Connection.php line 673
    at Connection->runQueryCallback('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), object(Closure)) in Connection.php line 629
    at Connection->run('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), object(Closure)) in Connection.php line 342
    at Connection->select('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), true) in Builder.php line 1508
    at Builder->runSelect() in Builder.php line 1494
    at Builder->get(array('*')) in Builder.php line 596
    at Builder->getModels(array('*')) in Builder.php line 303
    at Builder->get() in SearchController.php line 25
    at SearchController->getResults(object(Request))
    at call_user_func_array(array(object(SearchController), 'getResults'), array(object(Request))) in Controller.php line 80
    at Controller->callAction('getResults', array(object(Request))) in ControllerDispatcher.php line 146
    at ControllerDispatcher->call(object(SearchController), object(Route), 'getResults') in ControllerDispatcher.php line 94
    at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
    at ControllerDispatcher->callWithinStack(object(SearchController), object(Route), object(Request), 'getResults') in ControllerDispatcher.php line 54

2 个答案:

答案 0 :(得分:2)

首先检查$ query是否包含要搜索的字符串

User::where(DB::raw('CONCAT(first_name," ", last_name)'), 'like', '%'.$query.'%')->orwhere('username', 'like', '%'.$query.'%')->get();

答案 1 :(得分:0)

您的查询有语法错误和拼写错误。

CONCAT (fisrt_name, ' ', last_name), LIKE %s%

应该是:

CONCAT (first_name, ' ', last_name) LIKE %s%

你也可以这样做:

SELECT * FROM `users`
CONCAT (first_name, ' ', last_name) AS fullname
WHERE `username` LIKE %s%
HAVING fullname LIKE %s%