我正在尝试使用雄辩的方式将关联数组传递给where子句,但出现错误。
这里是我所传递的内容的示例
$where_array = [
'last_name' => 'Smith',
'first_name' => 'John'
];
$match = MyClass::where($where_array)->get();
这将返回错误,例如:
“ message”:“ SQLSTATE [42S22]:找不到列:1054” where子句中的未知列'{\“ last_name \”:\“ Smith \”,\“ first_name \”:\“ John \”}} '(SQL:从my_class
中选择*,其中{\"last_name\":\"Smith\",\"first_name\":\"John\"}
为空,my_class
。deleted_at
为空限制1)”,“ exception”:“ Illuminate \ Database \ QueryException”,“文件”:“ / home / vagrant / code / unity / vendor / laravel / framework / src / Illuminate / Database / Connection.php”,“ line”:669,
我不知道为什么它不接受数组或为什么要检查它是否为null。是否有一些我不包括的库使这种格式起作用?为什么查询中包含对象{}括号?它会变成某个地方的物体吗?
我的朋友,在where子句中,您需要传递至少2个参数,第一个是您要应用条件的模型中列的名称。
例如:我想在用户表中找到名为“侯赛因”的用户。
$user = User::where('name','hossein')->first();
根据文档,传递多个参数的正确格式如下:
您还可以将条件数组传递给where函数:
$ users = DB :: table('users')-> where([['状态','=','1'],['subscribed','<>','1'],])-> get();
根据您的情况,您需要将代码更改为:
$where_array = [
['last_name' ,'=', 'Smith'],
['first_name' ,'=', 'John'],
];
$match = MyClass::where($where_array)->get();