【发布时间】:2017-07-11 07:19:56
【问题描述】:
在 Laravel 5 中,我正在尝试进行客户查询。我的代码是这样的:
$params = 数组( '标准' => $标准, '标准1' => $标准 );
//Define the SQL
$sql = 'SELECT * FROM ' . $this -> _taskTableName .'
JOIN ' . $this -> _userTableName .' ON
' . $this -> _userTableName .'.id = ' . $this -> _taskTableName .' .client_id
WHERE notes LIKE \'%:criteria%\' OR name LIKE \'%:criteria1%\' ';
//Exeute the search
$tasks = DB::statement(DB::raw($sql),$params);
除了我不断收到此错误,即使我删除了 DB::raw,我也尝试过 DB::select。
SQLSTATE[HY093]: Invalid parameter number: :criteria (SQL: SELECT * FROM tasks
JOIN users ON
users.id = tasks .client_id
WHERE notes LIKE '%:criteria%' OR name LIKE '%:criteria1 %' )
in Connection.php (line 647)
at Connection->runQueryCallback(object(Expression), array('criteria' => 'Devin', 'criteria1' => 'Devin'), object(Closure))
in Connection.php (line 607)
at Connection->run(object(Expression), array('criteria' => 'Devin', 'criteria1' => 'Devin'), object(Closure))
in Connection.php (line 450)
有谁知道为什么会发生这种情况以及如何解决?
【问题讨论】:
-
你尝试过使用 eloquent 模型吗?查询看起来并不那么复杂(除非您为帖子简化),为什么要使用原始 sql 而不是使用模型?至于例外,似乎当您将变量包含在 % % 中时,它可能会将其视为标准本身而不是变量