mercredi 4 janvier 2017

Get specific column value from MySql in Laravel DB select function

How to I get a specific column value after fetching the results from MySql in Laravel DB select function

Below is my php code.

$list = DB::select('SELECT DISTINCT a.pincode FROM table_name nvs 
        JOIN areas a on nvs.area_id = a.id 
        where nvs.city_id = ?;', [$area_id]);

Below is the results for above query.

[
  {
    "pincode": "560005"
  },
  {
    "pincode": "560006"
  }
]

I need only pincode value not as key value pair. So i tried with the below code.

return array_column($list->toArray(), 'pincode');

It gives Call to a member function toArray() on array.

How can I get only values without using foreach loop.

Answer

I used the below code

$pincode_list = DB::table('table_name as nvs')
            ->join('areas as a', 'nvs.area_id', '=', 'a.id')
            ->select('a.pincode')
            ->where('nvs.city_id', '=', $area_id)
            ->distinct('a.pincode')->pluck('pincode')->toArray();

Result was :

[
    "560067",
    "560035"
]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire