Khagesh
Khagesh

Reputation: 191

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

db column screenshot

I want to fetch only two columns data from table 1st column data as array key and another column data as array value.

as array['wid'=>'temp']

result should be array['1'=>'1.5','2'=>'11.50']

for laravel 5.4

Upvotes: 0

Views: 1798

Answers (3)

Khagesh
Khagesh

Reputation: 191

This worked for me.

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

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

Upvotes: 0

KaziBablu
KaziBablu

Reputation: 533

Use Collection pluck() The pluck method retrieves all of the values for a given key:

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

For more information visit laravel doc here

Upvotes: 1

Rwd
Rwd

Reputation: 35180

You could use the pluck() method (scroll down to the Retrieving A List Of Column Values) e.g.

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

Upvotes: 1

Related Questions