php - 数组到字符串转换 laravel 7
问题描述
我需要在数组中获取没有字符串的值。这将是字段的名称:
static function showCust(){
$table = DB::table("dummy_db.customer")->select("*")->paginate(10);
$getFieldName = ["CUST_NAME", "CUST_CITY"];
foreach($table as $items){
$a[] = $items->$getFieldName[0];
}
dd($a);
}
但结果:
ErrorException
Array to string conversion.
解决方案
如果您尝试仅检索CUST_CITY
带有密钥的列表,则CUST_NAME
可以使用以下pluck()
方法:
$array = DB::table("dummy_db.customer")
->take(10)
->pluck('CUST_CITY','CUST_NAME');
推荐阅读
- spring-boot - spring.flyway.init-sqls 是否工作(如预期)?
- google-data-studio - 按客户显示最高销售额的月份和价值
- .net - Cybersource 发布请求总是得到会话超时
- sql - 查找来自两个不同数据库的两个相同表中写入的数据总数
- flutter - 未处理的异常:PlatformException(PERMISSION_DENIED,拒绝访问位置数据,null)
- java - 如何删除 Map 的键并转换为 List - Java
- node.js - 使用 Mongoose 在特定索引处更新数组
- javascript - 如何解决 Leaflet Popup 重叠?
- python - 如何在 pandas-bokeh 面积图中添加工具提示?
- javascript - 如何使用 vue 分析组件在 VueJS 上设置自定义变量