簡體   English   中英

在Laravel 4中為嵌套的口才關系添加約束

[英]Adding Constraints to Nested Eloquent Relationships in Laravel 4

我有3個相關的表格,如下所示:部分-> AgeGroups->團隊。

我可以獲取部分列表,並列出每個部分中的所有年齡段和團隊。 當前,該列表列出了所有部門和年齡組,而不管年齡組中是否存在任何團隊。

我正在向此查詢添加約束,以便僅獲取包含其中包含團隊的年齡段的部分的列表。 例如

  • 列出一個年齡段包含團隊的部分
  • 沒有任何agegroups一節將不被列入
  • 在沒有任何團隊的agegroup 不會上市

表格列為:

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-> agegroups-> Section Model 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');
}
} 

我認為您要解決所有這些錯誤。 您所擁有的大多數東西都是正確的,但是可以嘗試一些清潔的東西。 在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))->...

將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM