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?
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();
I used the following code snippet and it worked fine.
$data = json_decode(json_encode((array) $users), true);