Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

What is the issue exactly?

Last updated 1 year ago.
0

Let's say I have 3 classes A, B and C, A (PK_A, att1, att2) B (PK_B, att1, att2, att3) C (PK_C, att1, att2) and then the associative class is like this Associative (#PK_A, #PK_B, #PK_C, att1, att2)

the problem is that I don't know how to associate those 3 classes (A, B and C) all together

In the end I should have something like this A (1, 'value', 'value') , B (1, 'value', 'value', 'value') , C (1, 'value', 'value') and Associative (1, 1, 1, 'value', 'value')

The answer maybe simple but I never used n-ary relationships and I'm new to laravel (eloquent)

Last updated 1 year ago.
0

That's clear, but what is the use case. This is simply pivot table (and pivot object) and if you need to call it only together with one of A,B or C in the context of a relationship with one of the other 2, then it's trivial.

So desribe how you need to use that in order to get suggestions.

Last updated 1 year ago.
0

Let's imagine I want to create A, B and C objects and attach them all together, when using two classes (many to many) it's simple ( A->Bs()->attach(id) ) but how about the three all together at once ?

btw, thanks for answering my questions ;)

Last updated 1 year ago.
0

With 3 models it's just the same:

pivot table a_b_c: a_id,b_id,c_id

relations on A:
belongsToMany('B','a_b_c')->withPivot('c_id'); // if needed to know related C
belongsToMany('C','a_b_c')

and accordingly on B and C
Last updated 1 year ago.
0

Thank you jarektkaczyk.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

teeyo teeyo Joined 14 Mar 2014

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.