0

I want to show data from database: for every id_grup has many lapangan(court)

  $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);

output for above code

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

There is no value for children which might be like this.

[{"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 Answer 1

1

You are using wrong array braces. You shouldn't first initialize the children property of a group instead of that you can do it like this:

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;

  $l_arr = [];
  $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'];
      $l_arr[] = $l;
  }

  $g->children = $l_arr;
  $groups[] = $g;
}

return json_encode($groups);

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

thanks @Saumya Rastogi the children has return value, but var $lapangan_resouce result didn't match as groups. Maybe i wrong for calling data(s)
now i have output [{"id_group":"Group_1","name":"Lapangan Badminton","expanded":true,"eventHeight":25,"children":{"id_lapangan":2,"name":"Lapangan 2"}},{"id_group":"Group_2","name":"Lapangan Tenis","expanded":true,"eventHeight":25,"children":{"id_lapangan":2,"name":"Lapangan 2"}}] the children only give last value
@Ferdy - See my updated, answer, just push all the children in an array, and push the array inside group's child, as shown in the updated answer!
Done! I just have some change to call data for $lapangan_resource. Thanks

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.