4

I'm having some trouble doing eloquent database queries in Laravel. I have searched around a lot, but I don't know exactly what to search for to get my answer.

I'm making a web page in Laravel for kindergardens, and I need to get all children connected to a kindergarden in a single collection/array. The problem is that children are only connected to the kindergarden through their group(s). So the query needs to be able to get all children that are part of any group connected to the kindergarden.

The model relationships are like this:
A kindergarden has many groups, and groups belong to a kindergarden.
A group has many children, and children have many groups.

The best I've managed to do so far is this:

public function getChildren(){

    $kid = Session::get('kid');

    $k = Kindergarden::find($kid);
    $groups = $k->group;

    $children;

    foreach($groups as $g){
        $children = $children->merge($g->children);
    }

    $children = $children->toArray();

    return Response::json($children);
}

But this makes duplicates when children are in several groups in the same kindergarden. It also seems like an unnecessary complicated way to do it.

For a while I also tried to get the hasManyThrough-relationship to work, but it doesn't seem to work when there'a a many-to-many relationship and a pivot table involved.

I tried with this:

class Kindergarden extends Eloquent {
    public function children()
    {
        return $this->hasManyThrough('Children', 'Group', 'kid', 'gid');
    }
}

and then tried to call

Kindergarden::find(1)->children;

I'm sure there is a really simple way to do this, but I'm totally new to laravel and not really that great at sql, so I haven't been able to find anything to help me figure this out.

Edit: Managed to find a way to do it using Fluent:

$children = DB::table('children')
        ->join('children_group', 'children.chiid', '=', 'children_group.chiid')
        ->join('group', 'group.gid', '=', 'children_group.gid')
        ->where('group.kid', '=', $kid)
        ->groupby('children.chiid')
        ->get();

Still want to be able to do it using Eloquent, though.

1 Answer 1

6

In your Kindergarden class:

public function groups()
{
    $this->hasMany('Groups', 'group_id', 'id');
}

In your Group class:

public function children()
{
    $this->hasMany('Children', 'child_id', 'id')
}

Then try:

$children = Kindergarden::with('groups.children')->get();

This way you could go through each group in Kindergarden, and display each child that is apart of each group.

Also just a suggestion: In your database tables, I would name your related id's (such as a group ID in your Kindergarden table) group_id, instead of gid. Makes it much more readable and you know exactly what it's related to. Same with your child id, name it 'child_id' instead of 'chiid'.

EDIT: Since I don't know your table structure, take the functions above with a grain of salt. I hope what I suggested gets you on the right path! Let me know if it helps

1
  • If necessary I'd recommend adding the ->where('id', $my->id) clause for more direct searching. But this answer looks proper. +1
    – Andre F.
    Feb 13, 2017 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.