Laravel 拉威尔集合集合计数

Laravel 拉威尔集合集合计数,laravel,nested,eloquent,relationship,Laravel,Nested,Eloquent,Relationship,我试图从嵌套关系中获取记录计数。我想得到属于该领域的绵羊总数,以及农场的绵羊总数,但不知道如何在不迭代@foreach循环和更新变量的情况下实现这一点。雄辩是非常干净,所以我觉得有一个简单优雅的解决方案 谢谢 我有4个模型,农场,田地,牧羊人,绵羊,这是哈斯梅的关系 class Farm extends Model { protected $fillable = ['name, user_id']; public function fields() { ret

我试图从嵌套关系中获取记录计数。我想得到属于该领域的绵羊总数,以及农场的绵羊总数,但不知道如何在不迭代@foreach循环和更新变量的情况下实现这一点。雄辩是非常干净,所以我觉得有一个简单优雅的解决方案

谢谢

我有4个模型,农场,田地,牧羊人,绵羊,这是哈斯梅的关系

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>

应该是这样的

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 collections的文档。

我认为这意味着我需要在sheep模型中具有字段id键,以便此项工作,或者我可以从SHEERD模型继承此键吗?
$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
}