This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

lockzackary7643
lockzackary7643

I believe the plugin components automatically render arrays as JSON objects? Although i'm having a little trouble because the JSON Object in the response does not keep the array's keys intact.


public function getChildren($parent_id){
  $result = [];
  foreach (LocationModel::select('id','name')->where('parent_id','=',$parent_id)->get() as $loc) {
    $result[$loc->id] = $loc->name;
  }
  return $result;
}

i am expecting something like: {"1": "Orbis","34": "Ellinia","35": "Hennesys","62": "Sleepywood"} but the response I get is ["Orbis","Ellinia","Hennesys","Sleepywood"]

can someone help me how to output a JSON object the way I intended the JSON Object to be? I also tried using Laravel's Response::json($myarray); but the output becomes: ["0": "Orbis","1": "Ellinia","2": "Hennesys","3": "Sleepywood"]

Last updated

daftspunky
daftspunky

Give this a shot

return LocationModel::select('id','name')->where('parent_id','=',$parent_id)->lists('name', 'id');
lockzackary7643
lockzackary7643

yeah its the same response as return Response::json(array("1"=> "Orbis","34"=> "Ellinia","35"=> "Hennesys","62"=>"Sleepywood"))

the keys get discarded and the output received is simply: ["Orbis","Ellinia","Hennesys","Sleepywood"]

daftspunky
daftspunky

I am not sure why the keys are stripped, this is the behavior of Laravel.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.