Laravel 错误传递给 Illuminate\Database\Connection::prepareBindings() 的参数 1 必须是数组类型,字符串给定,调用

Laravel error Argument 1 passed to Illuminate\Database\Connection::prepareBindings() must be of the type array, string given, called in

我想工作的代码,但出现错误:

$test= 'test1';
$add = DB::insert('insert into clients (name,user_id,subdomain) values (?, "3", "test")', $test);

错误:

Argument 1 passed to Illuminate\Database\Connection::prepareBindings() must be of the type array, string given, called in C:\xampp\htdocs\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 454

此代码有效:

$add = DB::insert('insert into clients (name,user_id,subdomain) values ("name", "3", "test")');

如何让第一个示例中的代码起作用?

您需要将值作为数组传入。就像在它周围添加括号一样简单。

$add = DB::insert('insert into clients (name,user_id,subdomain) values (?, "3", "test")', [$test]);