Reputation: 526
This is my select statement:
$users = User::where("username", "LIKE", "%" . $query . "%")
->select("username")
->get();
This currently outputs as:
[{"username":"Test"}]
I need it to output in one of the following formats:
{ 'data': ['result1', 'result2', ... ] }
OR
['result1', 'result2', ...]
How would I do this?
Upvotes: 1
Views: 11058
Reputation: 406
$users = User::where("username", "LIKE", "%" . $query . "%")->select("username")->get()->toArray();
Upvotes: 6
Reputation: 6464
You can use plain PHP. Just reverse-json the object.
In your case, you can do $users=@json_decode(json_encode($users), true);
Or you can add a static method to your parent or current controller like this
public static function objectToArray(&$object)
{
return @json_decode(json_encode($object), true);
}
Upvotes: 0
Reputation: 526
Spoke too soon:
$users = User::where("username", "LIKE", "%" . $query . "%")
->lists("username");
return Response::json(array("data" => $users));
Upvotes: 2