2 years ago

Process data from pivot table relationships

Posted 2 years ago by lmartins


I'm looking to build an app where a organization can make multiple purchases of a given product, with every purchase adding a X amount of credits for that product to that organization.

So, the Schema looks like this:

        Schema::create('organizations', function (Blueprint $table) {

        Schema::create('products', function (Blueprint $table) {

        Schema::create('purchases', function (Blueprint $table) {

Then on the Organization controller I load the product model for each purchase:

    public function show(Organization $organization) {
        return view('organizations.show', compact('organization'));

Which then allows me to display a list of purchases like this:


  • 2 months ago | Prod A | 51 Credits
  • 4 months ago | Prod A | 62 Credits
  • 6 months ago | Prod B | 118 Credits
  • 8 months ago | Prod A | 346 Credits
  • 9 months ago | Prod C | 377 Credits

These credits will control actions the organization can do, so my goal now is first to get the total credits for each product, so I can easily display and interact with those total amounts.

How would you approach this?

Please sign in or create an account to participate in this conversation.