首页 > 解决方案 > 数组到字符串转换 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.

标签: phplaraveleloquentlaravel-7

解决方案


如果您尝试仅检索CUST_CITY带有密钥的列表,则CUST_NAME可以使用以下pluck()方法:

$array = DB::table("dummy_db.customer")
    ->take(10)
    ->pluck('CUST_CITY','CUST_NAME');


推荐阅读