Laravel显示阵列json

时间:2016-12-23 14:53:49

标签: php json laravel

我想显示数据库中的数据:因为每个id_grup都有很多lapangan(球场)

  $groups_resource = Groups::all();
  $groups = [];
  foreach($groups_resource as $group)
  {
    $g = new Groups();
    $g->id_group = "Group_".$group['id_group'];
    $g->name = $group['nama'];
    $g->expanded = true;
    $g->eventHeight = 25;
    $g->children = array();
    $groups[] = $g;

  $lapangan_resource = Lapangan::with('groups')->orderBy('nama')->get();
  foreach($lapangan_resource as $lapangan)
  {
    $l = new Lapangan();
    $l->id_lapangan = $lapangan['id_lapangan'];
    $l->name = $lapangan['nama_lapangan'];
    $g->children[] = $l;
  }
  }
    return json_encode($groups);

以上代码的输出

[{"id_group":"Group_1","name":"Lapangan Badminton","expanded":true,"eventHeight":25,"children":[]},{"id_group":"Group_2","name":"Lapangan Tenis","expanded":true,"eventHeight":25,"children":[]}]

儿童没有任何价值可能是这样的。

[{"id":"group_1","name":"Indoor","expanded":true,"eventHeight":25,"children":[
{"id":"1","name":"Court 1"},
{"id":"2","name":"Court 2"},
{"id":"3","name":"Court 3"},
{"id":"4","name":"Court 4"}]},"id":"group_2","name":"Outdoor","expanded":true,"eventHeight":25,"children":[
{"id":"11","name":"Court 5"},
{"id":"12","name":"Court 6"},
{"id":"13","name":"Court 7"},
{"id":"14","name":"Court 8"}]}]

1 个答案:

答案 0 :(得分:1)

您使用的是错误的数组括号。你不应该首先初始化一个组的WEB-INF属性而不是你可以这样做:

children

希望这有帮助!