2015-12-19 71 views
0

我想从嵌套关系中获取记录计数。我希望得到属于该领域的羊的总数,并且对于农场来说也是一样的,但不知道如何在不重复@foreach循环和更新变量的情况下完成此操作。雄辩是非常干净的,所以我觉得有一个简单的优雅的解决方案。laravel收藏集计数

感谢

我有4款车型,农场,场,牧羊人,羊这是hasMay关系

class Farm extends Model { 

    protected $fillable = ['name, user_id']; 

    public function fields() { 
     return $this->hasMany('App\Field'); 
    } 

    public function fieldCount() { 
     return $this->fields->count(); 
    } 

} 

class Field extends Model { 

    protected $fillable = ['name, farm_id, user_id']; 

    public function farm() { 
     return $this->belongsTo('App\farm'); 
    } 

    public function shepherds() { 
     return $this->hasMany('App\Shepherd'); 
    } 

    public function group() { 
     return $this->belongsTo('App\Group'); 
    } 

    public function shepherdCount() { 
     return $this->shepherds->count(); 
    } 

    public function shepherdSheepCount() { 
     return $this-> 
    } 

} 


class Shepherd extends Model { 

    protected $fillable = ['name, field_id, user_id']; 

    public function field() { 
     return $this->belongsTo('App\Field'); 
    } 

    public function sheep() { 
     return $this->hasMany('App\Sheep'); 
    } 

    public function sheepCount() { 
     return $this->sheep->count(); 
    } 

} 

class Sheep extends Model { 

    protected $fillable = ['name, shepherd_id, user_id']; 

    public function shepherd() { 
     return $this->belongsTo('App\Shepherd'); 
    } 

    public function meals() { 
     return $this->hasMany('App\Meal'); 
    } 

} 


#CONTROLLER METHOD 
public function test() 
{ 
    $Farms = Farm::with('fields', 
         'fields.shepherds', 
         'fields.shepherds.sheep')->get(); 
    return view('test')->with('farms', $Farms); 
} 



#VIEW 
        <ul> 
         @foreach ($farms as $farm) 
          <li>{{$farm->name}} Farm - {{$farm->fieldCount()}}</li> 

          <ul > 
          @foreach ($farm->fields as $field) 
           <li>{{$field->name}} Field - {{ $field->shepherdCount()}}</li> 
            <ul> 
             @foreach ($field->shepherds as $shepherd) 
              <li> 
              Shepherd {{ $shepherd->name }} has 
              {{ $shepherd->sheepCount() }} sheep 
              </li> 
             @endforeach 
            </ul>  
          @endforeach 
          </ul> 
         @endforeach 
        </ul> 

查看结果

Idaho Farm - 2 
    West Mervinland Field - 3 
     Shepherd Fermin has 61 sheep 
     Shepherd Clement has 54 sheep 
     Shepherd Ned has 57 sheep 
    North Mariellebury Field - 2 
     Shepherd Chadrick has 53 sheep 
     Shepherd Jackson has 61 sheep 
Washington Farm - 0 
California Farm - 4 
    Andersonport Field - 0 
    South Carmenborough Field - 3 
     Shepherd Alfonzo has 49 sheep 
     Shepherd Boris has 59 sheep 
     Shepherd Omari has 53 sheep 
    West Archibaldhaven Field - 4 
     Shepherd Darrin has 64 sheep 
     Shepherd Davion has 53 sheep 
     Shepherd Miller has 51 sheep 
     Shepherd Kristoffer has 59 sheep 
    Cassinmouth Field - 2 
     Shepherd Paxton has 50 sheep 
     Shepherd Hayley has 56 sheep 

回答

0

这是应该的

class Sheep extends Model { 

    //specify the table explicitly otherwise laravel will assume sheeps 
    protected $table = 'sheep'; 

    //sheep belongs to a field 
    public function field(){ 
     return $this->belongsTo('App\Field', 'foreign_key', 'primary_key'); 
    } 

} 

class Field extends Model { 


    //field belongs to a farm 
    public function farm() { 
     return $this->belongsTo('App\Farm', 'foreign_key', 'primary_key'); 
    } 

    //field has many sheep 
    public function sheep() { 
     return $this->hasMany('App\Sheep', 'foreign_key', 'primary_key'); 
    } 

} 

class Farm extends Model { 

    //farm has many fields 
    public function fields() { 
     return $this->hasMany('App\Field'); 
    } 

} 

得到这样

$farm->fields // would always return collection since 1-many relation so you //have to loop through it 
    foreach($fields as $field){ 
     $field->sheep;//this will also return collection 
} 

的结果,你可以将所有收集的方法对这些,大概count()是一个方法也看到laravel收藏文档获取更多信息。

+0

我认为这意味着我需要在羊模型中使用field_id键才能使其工作,或者我可以从牧羊人模型继承此键吗? – Basicmanthz

+0

是的,你必须相应地设置外键。 –

+0

完美的作品!谢谢:) – Basicmanthz