How to GROUP and SUM a pivot table column in Eloquent relationship?

Thomas Jensen

In Laravel 4; I have model Project and Part, they have a many-to-many relationship with a pivot table project_part. The pivot table has a column count which contains the number of a part ID used on a project, e.g.:

id  project_id  part_id count
24  6           230     3

Here the project_id 6, is using 3 pieces of part_id 230.

One part may be listed multiple times for the same project, e.g.:

id  project_id  part_id count
24  6           230     3
92  6           230     1

When I show a parts list for my project I do not want to show part_id twice, so i group the results.

My Projects model has this:

public function parts()
{
    return $this->belongsToMany('Part', 'project_part', 'project_id', 'part_id')
         ->withPivot('count')
         ->withTimestamps()
         ->groupBy('pivot_part_id')
}

But of course my count value is not correct, and here comes my problem: How do I get the sum of all grouped parts for a project?

Meaning that my parts list for project_id 6 should look like:

part_id count
230     4

I would really like to have it in the Projects-Parts relationship so I can eager load it.

I can not wrap my head around how to do this without getting the N+1 problem, any insight is appreciated.


Update: As a temporary work-around I have created a presenter method to get the total part count in a project. But this is giving me the N+1 issue.

public function sumPart($project_id)
{
    $parts = DB::table('project_part')
        ->where('project_id', $project_id)
        ->where('part_id', $this->id)
        ->sum('count');

    return $parts;
}
Razor

From the code source:

We need to alias all of the pivot columns with the "pivot_" prefix so we can easily extract them out of the models and put them into the pivot relationships when they are retrieved and hydrated into the models.

So you can do the same with select method

public function parts()
{
    return $this->belongsToMany('Part', 'project_part', 'project_id', 'part_id')
        ->selectRaw('parts.*, sum(project_part.count) as pivot_count')
        ->withTimestamps()
        ->groupBy('project_part.pivot_part_id')
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Eloquent relationship in custom pivot table

From Dev

Eloquent many to many relationship access table column value from pivot table

From Dev

How to sum and group a table by a specific column

From Dev

Laravel Eloquent group by with pivot table & relations

From Dev

Pivot Table : How to group by date if the column headers are month names?

From Dev

How to group a column which is in hours by days and months in a pivot table?

From Dev

Relationship with Pivot Table Laravel's Eloquent and Inner Loops

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Sum of row and column in SQL pivot table

From Dev

Pivot SQL Table rows to 3 column with sum

From Dev

Sum of row and column in SQL pivot table

From Dev

Laravel Eloquent sum of column in another table

From Dev

How to sort by primary table field and pivot in Eloquent?

From Dev

SQL How to Update SUM of column over group in same table

From Dev

SQL How to Update SUM of column over group in same table

From Dev

How to specify pivot table for hasOne() relationship in Laravel

From Dev

Laravel eloquent relationship and pivot tables

From Dev

laravel eloquent pivot table query "ambiguous column name"

From Dev

laravel eloquent pivot table query "ambiguous column name"

From Dev

Eloquent, pivot table data

From Dev

Laravel eloquent with pivot table

From Dev

Eloquent sort by relationship column

From Dev

Laravel relationship with pivot table

From Dev

Eloquent - Where on relationship table

From Dev

Eloquent relation pivot table with table

From Dev

Eloquent relation pivot table with table

From Dev

Laravel / Eloquent hasMany relationship sum()

From Dev

How to group dates in pivot table day wise?

Related Related

  1. 1

    Eloquent relationship in custom pivot table

  2. 2

    Eloquent many to many relationship access table column value from pivot table

  3. 3

    How to sum and group a table by a specific column

  4. 4

    Laravel Eloquent group by with pivot table & relations

  5. 5

    Pivot Table : How to group by date if the column headers are month names?

  6. 6

    How to group a column which is in hours by days and months in a pivot table?

  7. 7

    Relationship with Pivot Table Laravel's Eloquent and Inner Loops

  8. 8

    Laravel Eloquent Many to Many relationship using a pivot table

  9. 9

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  10. 10

    Sum of row and column in SQL pivot table

  11. 11

    Pivot SQL Table rows to 3 column with sum

  12. 12

    Sum of row and column in SQL pivot table

  13. 13

    Laravel Eloquent sum of column in another table

  14. 14

    How to sort by primary table field and pivot in Eloquent?

  15. 15

    SQL How to Update SUM of column over group in same table

  16. 16

    SQL How to Update SUM of column over group in same table

  17. 17

    How to specify pivot table for hasOne() relationship in Laravel

  18. 18

    Laravel eloquent relationship and pivot tables

  19. 19

    laravel eloquent pivot table query "ambiguous column name"

  20. 20

    laravel eloquent pivot table query "ambiguous column name"

  21. 21

    Eloquent, pivot table data

  22. 22

    Laravel eloquent with pivot table

  23. 23

    Eloquent sort by relationship column

  24. 24

    Laravel relationship with pivot table

  25. 25

    Eloquent - Where on relationship table

  26. 26

    Eloquent relation pivot table with table

  27. 27

    Eloquent relation pivot table with table

  28. 28

    Laravel / Eloquent hasMany relationship sum()

  29. 29

    How to group dates in pivot table day wise?

HotTag

Archive