【发布时间】:2014-12-14 00:09:32
【问题描述】:
我想在我的 Eloquent 查询中使用 WhereIn 函数,它需要一个 PHP 数组:
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
目前,数据正在以这种格式传入控制器 -
array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }
我希望将上述数据转换为(1,4,6) 格式以使用该功能,但我不确定如何。
目前正在以下代码中使用(bedrooms和property_type以上述格式显示):
$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');
这会返回一个Array to string conversion 错误。
我的 Laravel 版本是 4.2.* - dev.
任何帮助将不胜感激。
【问题讨论】:
-
如果我下面的解决方案不起作用,请发布您的整个控制器。
-
我不相信 whereIn 需要 3 个参数。您正在尝试在 whereIn 语句中将 '=' 强制转换为数组,删除它,它应该可以工作,或者至少可以将数组转换为字符串错误。让我知道,我会在下面更新我的答案。
-
正是以上!谢谢@mschuett!你有一个细节的眼睛。删除第三个参数解决了这个问题并返回了预期的结果。
标签: php arrays json laravel eloquent