0

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]

3
  • 1
    So the data is stored as a json encoded string?? Commented Jun 17, 2016 at 7:29
  • 1
    From mysql 5.7 you can use JSON data type dev.mysql.com/doc/refman/5.7/en/json.html Commented Jun 17, 2016 at 7:29
  • 1
    Just the ID value take for another query, so has to remove BeneficiaryID part. Commented Jun 17, 2016 at 7:47

1 Answer 1

1
 $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();
Sign up to request clarification or add additional context in comments.

5 Comments

json_decode() expects parameter 1 to be string, array given
You're getting the result using toJson() method?
because in my answer a string is passed as parameter to json_decode function and the laravel toJson method is returning string as well? But if you don't need the json format you can use lists() method that will return an array
If we supply '[{"BeneficiaryID":"2"},{"BeneficiaryID":"3"},{"BeneficiaryID":"4"},{"BeneficiaryID":"6"}]', we are getting the correct output. but we are getting this as a variable, when its used $data = json_decode($variable); we are getting errors.
that variable in JSON format. this is the result [{"BeneficiaryID":"2"},{"BeneficiaryID":"3"},{"BeneficiaryID":"4"},{"Beneficiar‌​yID":"6"}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.