在 Laravel 4 中为嵌套的雄辩关系添加约束


Adding Constraints to Nested Eloquent Relationships in Laravel 4

我有 3 个相关表格,如下所示 部分 -->年龄组 --> 团队。

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

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

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

表列为:

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 -> 年龄组 -> 部分模型年龄组与团队 () 方法。

有什么想法吗?!(谢谢)

// 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');
}
} 

我认为你做错了。您现有的大部分内容都是正确的,但请尝试更干净一些。部分内部.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))->...

将 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#_listshttp://laravel.com/docs/queries