Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

What you are trying to do here is well within the realm of possibility, but it's not clear exactly what you are trying to achieve. Can you explain the relationships and roles you are trying to set up are in plain english?

Here's what I gather from what you've shown:

  • One Company can have many Branches, (and inversely, a Branch can only belong to one Company)
  • One Company can have many Products, (and inversely, a Product can only belong to one Company)
  • A Product Variation has one "parent" Product (that it is based off of), (and inversely, a Product can have many Product Variations)

The part that is currently unclear is what your BranchProductVariation table's purpose is. What is it trying to represent in real life? I think it is to show that Product Variation's can belong to a Branch, but the way it's laid out right now, a Product Variation can belong to a Branch that isn't actually part of a Company that makes the parent Product.

0

Hi camroncade,

Yes you have it spot on.

  • One Company can have many Products and a Product will only belong to one Company.
  • A Product Variation has one "parent" Product and a Product can have many Product Variations.

The Branch Product Variation is where it gets complicated for me. What I would like is to be able to store stock qty for each Product Variation by Branch. A Company may have many Branches and each Branch will have different levels of stock of each Product Variation?

I hope that makes sense?

0

Yeah, this is making more sense. Product Variations will have quantities within a Branch, and the Product table represents more of a template and not what would be a physical product that would have a quantity. Is this correct?

0

Yes exactly correct

0

If you're looking to query the Products that belong to a Branch, you could do something like this:

// on the Branch model
public function products()
{
    return $this->hasMany('App\Product', 'company_id', 'company_id');
}
0

That works, but doesn't seem to load the ProductVariations or the pivot, my code to get the product;

    $product = \Branch::findOrFail(1)->products()->where('id',1)->get();
Last updated 8 years ago.
0

What relations do you have setup on the ProductVariations model?

0

currently I have;

public function branch()
{
    return $this->belongsToMany('Branch')->withPivot('qty');
}

public function product()
{
    return $this->belongsTo('Product');
}
0

Ok. What relations do you have on the Product model?

0
public function company()
{
    return $this->belongsTo('Company');
}

public function variations()
{
    return $this->hasMany('ProductVariation');
}
0

Update...

    $product = \Branch::with('productVariation')->findOrFail(1)->products()->where('id',1)->first();

My query now gets the variations, but no pivot columns, I can see the SQL query gets all the right info, but I can't access it using;

            @foreach($product->variations as $variation)
                {{ $variation->name }} <br />
                {{ $variation->pivot->qty }} <br />    // errors with Trying to get property of non-object
            @endforeach
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ale1981 ale1981 Joined 6 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.