[英]Array to string conversion laravel 7
i need to get the values without string in array.我需要在数组中获取没有字符串的值。 this will be name of fields:
这将是字段的名称:
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);
}
but the results:但结果:
ErrorException Array to string conversion.
If you are trying to retrieve only the list of CUST_CITY
with the key CUST_NAME
you can use the pluck()
method:如果您尝试仅使用键
CUST_NAME
检索CUST_CITY
列表,则可以使用 pluck pluck()
方法:
$array = DB::table("dummy_db.customer")
->take(10)
->pluck('CUST_CITY','CUST_NAME');
The error msg occurs because $getFieldName
as a variable, object call the variable without [0]
, you need to wrap $getFieldName[0]
with brace:出现错误消息是因为
$getFieldName
作为变量,object 调用没有[0]
的变量,您需要用大括号包裹$getFieldName[0]
:
$items->{$getFieldName[0]};
You are applying paginate
to query-builder, the result will be Illuminate\Pagination\LengthAwarePaginator
object.您正在将
paginate
应用于查询构建器,结果将是Illuminate\Pagination\LengthAwarePaginator
object。
If you want to get the customer objects inside.如果你想把客户对象放在里面。 You can use
getCollection()
method to $table
:您可以对
$table
使用getCollection()
方法:
foreach($table->getCollection() as $items){
$a[] = $items->${getFieldName[0]};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.