laravel中如何对内部关系模型进行计数和求和

How to count and sum inner relational model in laravel

我正在 Laravel 中构建一个小型应用程序,我在其中遇到了内部关系数据的总和问题,

我有一个模型 Company has Many 关系 associatedProjectsassociatedProjects belongs to 关系 projectproject hasOne technicalDescription.

公司型号:

class Company extends Model {

    public function roles()
    {
        return $this->belongsToMany('Noetic\Plugins\Conxn\Models\Variables\Company\Role', 'company_role_relation', 'company_id', 'role_id')->withTimestamps();
    }

    public function specialisations()
    {
        return $this->belongsToMany('Noetic\Plugins\Conxn\Models\Variables\Company\Role', 'company_specialisation_relation', 'company_id', 'specialisation_id')->withTimestamps();
    }

    public function associatedProjects()
    {
        return $this->hasMany('Noetic\Plugins\Conxn\Models\Project\AssociateCompany','company_id','id');
    }

}

关联公司 型号:

class AssociateCompany extends Model {

    protected $table = 'project_associate_company';

    protected $fillable = [
        'project_id', 'company_role_id', 'company_specialisation_id', 'company_id', 'link', 'file_name'
    ];

    public function project()
    {
        return $this->belongsTo('Noetic\Plugins\Conxn\Models\Project','project_id','id');
    }

    public function company()
    {
        return $this->belongsTo('Noetic\Plugins\Conxn\Models\Company','company_id','id');
    }

    public function companyRole()
    {
        return $this->belongsTo('Noetic\Plugins\Conxn\Models\Variables\Company\Role',
            'company_role_id','id');
    }

    public function specialisation()
    {
        return $this->belongsTo('Noetic\Plugins\Conxn\Models\Variables\Company\Role',
            'company_specialisation_id','id');
    }
}

项目模型

class Project extends Model {

    protected $fillable = [
        'user_id','koshy_id', 'name', 'slug', 'owner_spv', 'spv_link', 'latitude', 'longitude',
        'landmark', 'city', 'district', 'state', 'pin_code', 'region_id', 'country', 'building_use',
        'sector', 'conxn_id', 'parent_project_id', 'website', 'project_logo', 'tracked', 'verified',
        'code_link', 'status', 'active', 'premium','area'
    ];

    public function technicalDescription()
    {
        return $this->hasOne('Noetic\Plugins\Conxn\Models\Project\TechnicalDescription','project_id','id');
    }

    public function associateCompany()
    {
        return $this->hasMany('Noetic\Plugins\Conxn\Models\Project\AssociateCompany','project_id','id');

    }

}

现在这个 technicalDescription 有字段 construction_cost,现在我想先计算 associatedProject 的总数并获取所有 project 的总和 construction_costtechnicalDescription 中,我在这段代码中做了一些事情:

$company = Company:: where( 'status', 'saved')
    ->withCount( 'associatedProjects' )
    ->with('associatedProjects.project.technicalDescription')
    ->get()
    ->transform(function ($value)   {
        $value['project_value'] = $value['associatedProjects']->flatten(2)
            ->pluck('project.technicalDescription')->sum('construction_cost');
        return  $value;
    })
    ->sortByDesc('project_value')
    ->forpage( $request->page , 10 );

$next = $request->page+1 ;
$previous =$request->page-1 ? abs($request->page-1):1 ;

我这里无法使用paginate,因为laravel集合没有这样的方法,而且查询逻辑也不准确。

欢迎提出任何建议。谢谢

您可以使用 BelongsToMany 关系直接得到 technicalDescriptions:

class Company extends Model {
    public function technicalDescriptions() {
        return $this->belongsToMany(
            'Noetic\Plugins\Conxn\Models\Project\TechnicalDescription',
             'project_associate_company',
             'company_id',
             'project_id',
             null,
             'project_id'
        );
    }
}

$company = Company::where('status', 'saved')
    ->withCount(['technicalDescriptions as project_value' => function($query) {
        $query->select(DB::raw('sum(construction_cost)'));
    }])
    ->orderByDesc('project_value')
    ->paginate();