Shani
Shani

Reputation: 99

How to use JSON object to mysql query?

From a recent query I am getting this as the result.

[{"BeneficiaryID":"2"},{"BeneficiaryID":"3"},{"BeneficiaryID":"4"},{"BeneficiaryID":"6"}]

I want to use these ID values for another mysql query using Laravel.
How can I just get only those numbers.
Final result should be like [2,3,4,6]

Upvotes: 0

Views: 537

Answers (1)

Calin Blaga
Calin Blaga

Reputation: 1373

 $data = json_decode('[{"BeneficiaryID":"2"},{"BeneficiaryID":"3"},{"BeneficiaryID":"4"},{"BeneficiaryID":"6"}]');

$id_array = [];

foreach($data as $data) {
    $id_array[] = $data->BeneficiaryID;
}

$query = Model::whereIn('column', $id_array)->get();

Upvotes: 1

Related Questions