0

I have the following JSON object returned from Laravel query:

Laravel code:

$users = DB::table('orders')
    ->where('custid','=', $cid)
    ->orderBy('id', 'desc')
    ->get();



[{"id":37,"date":"2016-11-30 19:16:30","status":"Processing","prodid":"23","custid":"46","created_at":"2016-11-30 19:16:30","updated_at":"2016-11-30 19:16:30"},
{"id":36,"date":"2016-11-30 18:59:29","status":"Processing","prodid":"23","custid":"46","created_at":"2016-11-30 18:59:29","updated_at":"2016-11-30 18:59:29"}]

I want to store the 'id' of each entry to a php array. I tried the following:

JSON_decode($users,TRUE);

To decode the JSON into a PHP associative array but I keep getting error 500. Any work arounds?

9
  • Can you provide the associated log? Commented Nov 30, 2016 at 19:37
  • Have you tried JSON_decode($users[0],TRUE);? Commented Nov 30, 2016 at 19:37
  • @Wistar Where can I find the log? I am hosted on EC2 Commented Nov 30, 2016 at 19:38
  • @Arihent in your laravel folder /storage/logs/laravel.log Commented Nov 30, 2016 at 19:39
  • @Wistar Tried JSON_decode($users[0],TRUE); still no luck. Looking into the logs now Commented Nov 30, 2016 at 19:40

2 Answers 2

1

The builder's get method returns a collection, which has a toArray method. You can use the select method to get just the id, if that's all you need.

$users = DB::table('orders')
    ->select('id')
    ->where('custid','=', $cid)
    ->orderBy('id', 'desc')
    ->get()
    ->toArray();
Sign up to request clarification or add additional context in comments.

Comments

0

I used the following code snippet and it worked fine.

$data = json_decode(json_encode((array) $users), true);

Comments

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.