Laravel 5.4 从数据库中仅获取 2 列数据作为数组键和值

Laravel 5.4 get only 2 column data from db as array key and value

db column screenshot

我只想从 table 中获取两列数据,第一列数据作为数组键,另一列数据作为数组值。

作为数组['wid'=>'temp']

结果应为数组['1'=>'1.5','2'=>'11.50']

对于laravel 5.4

您可以使用 pluck() 方法(向下滚动到 检索列值列表 ),例如

$data = DB::table('city_list')->pluck('city_name', 'cid');

使用集合 pluck() pluck 方法检索给定键的所有值:

$data = DB::table('city_list')->pluck('city_name','cid');

有关详细信息,请访问 laravel doc here

这对我有用。

$data = DB::table('city_list')->select('cid','city_name')->get(); 
$val = array();

foreach ($data as $key => $value) { 
    $val[$value->cid]=$value->city_name; 
}