Rhokai
Rhokai

Reputation: 373

How to get only JSON object values from a JSON array in LARAVEL 5.2

My database query is below.

$beneficiary_id = DB::select('select Telephone from item ');

This returns a json array looks like this

[{"Telephone":"0111222333"},{"Telephone":"0112211223"},{"Telephone":"012345557"},{"Telephone":"0225545455"}]

For another database insertion operation I need only the telephone numbers. I have used json_decode() function, it is working only if we enter the array manually.

How to get only those values to another array?

Thanks in advance.

Upvotes: 0

Views: 2042

Answers (2)

Michal Bieda
Michal Bieda

Reputation: 939

Use the pluck function

If you would like to retrieve an array containing the values of a single column, you may use the pluck method.

$titles = DB::table('roles')->pluck('title');

foreach ($titles as $title) {
    echo $title;
}

Upvotes: 2

Tony
Tony

Reputation: 21

use $beneficiary_id->column_name to get the value from your object.

Upvotes: 0

Related Questions