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?
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.
Hi camroncade,
Yes you have it spot on.
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?
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?
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');
}
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();
currently I have;
public function branch()
{
return $this->belongsToMany('Branch')->withPivot('qty');
}
public function product()
{
return $this->belongsTo('Product');
}
public function company()
{
return $this->belongsTo('Company');
}
public function variations()
{
return $this->hasMany('ProductVariation');
}
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community