gpt4 book ai didi

php - whereIn数组到字符串转换错误 - Laravel

转载 作者:行者123 更新时间:2023-12-02 01:02:49 28 4
gpt4 key购买 nike

我想在 Eloquent 查询中使用 WhereIn 函数,它需要一个 PHP 数组:

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();

目前,数据正以这种格式传递到 Controller 中 -

array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }

我希望将上述数据转换为 (1,4,6) 格式以使用该函数,但我不确定如何转换。

目前正在以下代码中使用(卧室和 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');

这会返回一个数组到字符串的转换错误。

我的 Laravel 版本是 4.2.* - dev。

如有任何帮助,我们将不胜感激。

最佳答案

没有必要这样做。您是否尝试过不将它们转换为整数?如果您无法完成这篇文章,那么您使用的是什么版本的 laravel,这样以后遇到这个问题的人就不会做不必要的工作。在 laravel 4.2.* 中,以下代码将返回正确的行。我刚刚对此进行了测试。

Route::get('/', function(){

$t = DB::table('users')->whereIn('id', array('1','2','3'))->get();

dd($t);

});

这也行。

Route::get('/', function(){

$t = DB::table('users')->whereIn('id', array(1,2,3))->get();

dd($t);

});

此外还有这段代码

array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "6" }

和这段代码完全一样。刚刚使用 var_dump 显示了一个。

array("1", "4", "6")

除非您确实将未序列化的数组存储在数据库中,否则您会遇到很多其他问题。

编辑:发布 Controller 后

我不相信 whereIn 需要 3 个参数。您正试图在 whereIn 语句中将“=”强制转换为一个数组,请删除它,它应该可以工作,或者至少可以避免 Array to string 错误。

Link to whereIn Laravel function.

关于php - whereIn数组到字符串转换错误 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441444/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com