ds20021205 2014-03-07 15:53
浏览 60

Laravel雄辩的查询与几层模型关系

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条回答 默认 最新

  • douwei7203 2014-03-08 16:25
    关注

    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', '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

    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题