whereIn 数组到字符串转换错误 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26441444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
whereIn array to string convertion error - Laravel
提问by mschuett
I am wanting to use the WhereIn
function on my Eloquent query, and it requires a PHP array:
我想WhereIn
在我的 Eloquent 查询中使用该函数,它需要一个 PHP 数组:
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
Currently, the data is being passed into the controller in this format -
目前,数据正在以这种格式传递到控制器中 -
array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }
I am wanting the above data to be casted to (1,4,6)
format to utilise the function but I'm unsure how.
我希望将上述数据转换为(1,4,6)
格式以利用该功能,但我不确定如何。
This is currently being used in the following code (bedrooms and property_type are shown in the above format):
这目前在以下代码中使用(卧室和属性类型以上述格式显示):
$bedrooms = Input::get('bedrooms');
$property_type = Input::get('property_type');
$locations = Location::whereHas('coordinate', function ($q) use ($minlat, $maxlat,
$minlng, $maxlng, $bedrooms, $property_type)
{
$q->whereBetween('lat', array($minlat, $maxlat))
->whereBetween('lng', array($minlng, $maxlng))
->whereIn('bedrooms', '=', $bedrooms)
->whereIn('type', '=', $property_type);
})->lists('id');
This returns a Array to string conversion
error.
这将返回一个Array to string conversion
错误。
My Laravel version is 4.2.* - dev.
我的 Laravel 版本是 4.2.* - dev。
Any help would be hugely appreciated.
任何帮助将不胜感激。
回答by mschuett
There is no need to do this. Have you tried it without casting them to ints? If you are not able to do this post what version of laravel you are using so people who come across this later don't do needless work. In laravel 4.2.* the following code will return the proper rows. I have just tested this.
没有必要这样做。您是否尝试过而不将它们转换为整数?如果您无法完成这篇文章,那么您使用的是哪个版本的 laravel,那么以后遇到这个问题的人就不会做不必要的工作。在 laravel 4.2.* 下面的代码将返回正确的行。我刚刚测试了这个。
Route::get('/', function(){
$t = DB::table('users')->whereIn('id', array('1','2','3'))->get();
dd($t);
});
This also works.
这也有效。
Route::get('/', function(){
$t = DB::table('users')->whereIn('id', array(1,2,3))->get();
dd($t);
});
Furthermore this code you have
此外,您拥有的此代码
array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }
And this code are exactly the same. One has just being displayed using var_dump.
而这段代码是完全一样的。一个刚刚使用 var_dump 显示。
array("1", "4", "6")
Unless you are literally storing unserialized arrays in your database in which case you have a ton of other issues.
除非您实际上是在数据库中存储未序列化的数组,在这种情况下您还有很多其他问题。
EDIT: After controller was posted
编辑:发布控制器后
I don't believe whereIn takes 3 parameters. You are trying to cast '=' to an array in your whereIn statements remove this and it should work or at least get ride of the Array to string error.
我不相信 whereIn 需要 3 个参数。您正在尝试将 '=' 转换为 whereIn 语句中的数组,删除它,它应该可以工作,或者至少可以避免 Array 到字符串错误。
回答by hannesvdvreken
First: I think Laravel can handle string typed items to be put in a sql where clause where the column is an integers.
第一:我认为 Laravel 可以处理字符串类型的项目,这些项目要放在 sql where 子句中,其中列是整数。
Second: if it doesn't work you can always 'cast' those strings to integers with the array_map
method.
第二:如果它不起作用,您总是可以使用该array_map
方法将这些字符串“转换”为整数。
More information here: http://php.net/manual/en/function.array-map.php
更多信息在这里:http: //php.net/manual/en/function.array-map.php
In your example:
在你的例子中:
$ids = array('1', '4', '6');
$ids = array_map(function ($value) {
return (int) $value;
}, $ids);
$users = DB::table('users')->whereIn('id', $ids)->get();
Hope this helps.
希望这可以帮助。