Laravel returning null value with where method (if variable is passed)
我正在尝试在 laravel 查询中使用 where 方法。我有一个包含两个值的字符串(用逗号分隔)。我需要使用逗号后的值进行搜索。所以我使用 explode php 函数来创建一个数组。所以我得到一个包含两个键值对的数组。我想使用第二个值来搜索数据库。所以我将第二个值存储在一个变量中,然后在 where 方法中传递该变量。但它返回空白集合对象
这是代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
$vehicles_name_trim_ar = explode(‘,’, Input ::get(‘vehicles_name_trim’));
print_r of $vehicles_name_trim_ar is
Array
(
[0] => A3
[1] => 2.0T Premium Automatic
)
//storing both values in seperate variable
$model_name = $vehicles_name_trim_ar[0];
$model_trim = $vehicles_name_trim_ar[1];
$model = Model::where(‘model_trim’, $model_trim)->get();
|
它返回空白结果。但是,如果我证明静态值,它会返回结果
1
|
$model = Model::where(‘model_trim’,“2.0T Premium Automatic”)->get();
|
我做错了什么?
- 你最好使用 var_dump 而不是 print_r 因为它会更清楚explode函数的结果是什么。
您在第二个值的开头有一个空格。试试这个:
1 2
|
$model_name = trim($vehicles_name_trim_ar[0]);
$model_trim = trim($vehicles_name_trim_ar[1]);
|
来源:https://www.codenong.com/25008912/
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8790.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57