在Laravel 4中为嵌套Eloquent关系添加约束

时间:2013-11-04 02:13:04

标签: php laravel-4 constraints relationship eloquent

我有3个相关的表格如下: - > AgeGroups - >队。

我能够获得部分列表并列出每个部分中的所有年龄组和团队。目前,无论年龄组中是否存在任何团队,都会列出所有章节和年龄组。

我希望为此查询添加约束,以便我只获得包含其中包含团队的年龄组的部分列表。例如

  • 包含团队的年龄组的部分将列出
  • 没有任何年龄组的部分不会
  • 列出
  • 没有任何队伍的年龄组不会
  • 列出

表格列是:

sections:   id | name
agegroups:  id | name  | section_id
teams:      id | name  | agegroup_id 

模型定义如下:

class Section extends Eloquent{
  public function agegroup()
  {
      return $this->hasMany('agegroup');
  }
}

class Agegroup extends Eloquent{
  public function section()
  {
        return $this->belongsTo('section');
  }

  public function team()
  {
        return $this->hasMany('team');
  }
}

class Team extends Eloquent {
  public function agegroup()
  {
    return $this->belongsTo('agegroup');
  } 
}

(出于测试目的)我的PHP代码包括用于预先加载的嵌套关系,位于下面的路由闭包中:

Route::get('/', function()
{
    $sections =  Section::with('agegroup.team')->get();

 foreach ($sections  as $section) {
echo "<h2>$section->name </h2><ul>";

foreach ($section->agegroup as $agegroup) {
    echo "<li>$agegroup->name </li><ul>";

    foreach ($agegroup->team as $team) {
    echo "<li> $team->name </li>";
    }
    echo "</ul>";
 }
 echo "</ul>";
}

});

在测试中,我已经能够为简单关系添加约束,但我对如何为嵌套关系执行此操作感到茫然。

我希望我已经正确解释了这一点,感谢您抽出时间阅读本文以及提供的任何帮助。

======更新======

在下面的帮助之后,我有以下代码,它似乎与建议的解决方案相同,禁止在模型中添加return语句以返回函数值:

这几乎就在那里 - 返回部分(用dd($ ...)检查)但是我得到错误:未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ teams

行$ teams = $ this - &gt;年龄组 - &gt; Section模型agegroupsWithTeams()方法中的团队。

任何想法?! (感谢)

// controller/view code
Route::get('/', function()
{
$sections = Section::allWithAgeGroupsWithTeams();

foreach ($sections as $section)
{
    echo $section->name

    foreach ($section->agegroupsWithTeams() as $agegroup)
    {
        echo $agegroup->name;

        foreach ($agegroup->teams as $team)
        {
            echo $team->name;
        }
    }
}
});

// model code 

class Section extends Eloquent
{
public function agegroups()
{
    return $this->hasMany('agegroup');
}

static function allWithAgeGroupsWithTeams()
    {
         $teams = Team::all();
         $agegroups = Agegroup::whereIn('id', $teams->lists('agegroup_id'))->get();
         $sections = Section::whereIn('id', $agegroups->lists('section_id'))->get();             
         return $sections;
    }

    public function agegroupsWithTeams()
    {

        $teams = $this->agegroups;
        return Agegroup::whereIn('id', $teams->lists('agegroup_id'))->get();
    }
}


class Agegroup extends Eloquent
{
public function section()
{
    return $this->belongsTo('section');
}

public function teams()
{
    return $this->hasMany('team');
}
} 

class Team extends Eloquent
{
public function agegroups()
{
    return $this->belongsTo('agegroup');
}
} 

2 个答案:

答案 0 :(得分:2)

我认为你这一切都错了。你所拥有的大部分内容都是正确的,但尝试一些更清洁的东西。在Section.php里面

class Section
...
    public function nestedgroups()
    {
        return $this->hasMany('Agegroup')->has('teams');
    }

然后您可以使用

简单地获取所有嵌套数据
$sections = Section::has('nestedgroups')->with('agegroups.teams')->get();

这适用于4.1和4.0。如果您使用4.1和sqlite进行测试,请确保以has()方式转义查询:

...::has('nestedgroups', '>', DB::raw(0))->...

答案 1 :(得分:0)

将agegroup()更改为agegroups()和team()更新为teams(),然后尝试这样的事情(代码未测试):

<?php

// model classes

class Section extends Eloquent {

    static function allWithAgeGroupsWithTeams()
    {
        $teams = Team::all();

        $agegroups = Agegroup::whereIn('id', $teams->lists('agegroup_id'))->get();

        $sections = Section::whereIn('id', $agegroups->lists('section_id'))->get();
    }

    public function agegroupsWithTeams()
    {
        $teams = $this->agegroups->teams;

        return Agegroup::whereIn('id', $teams->lists('agegroup_id'))->get();
    }

}


?>


<?php

// controller/view code

$sections = Section::allWithAgeGroupsWithTeams();

foreach ($sections as $section)
{
    echo $section->name;

    foreach ($section->agegroupsWithTeams() as $agegroup)
    {
        echo $agegroup->name;

        foreach ($agegroup->teams as $team)
        {
            echo $team->name;
        }
    }

}

?>

http://laravel.com/api/class-Illuminate.Support.Collection.html#_lists http://laravel.com/docs/queries