Relation manager with intermediate relationship

Hi Let's say I've an entity Foo that has many Bar, where Bar belongs to many Baz. Is that possible to have a relation manager for Foo listing the related Baz? 🤔 As far as I know, relation managers are based on Laravel Eloquent's relationships, and this isn't a base relationship. I cleary see how to retrieve these records using SQL joins, but not how to make it works with the awesome relation manager feature. Thanks 🙏
12 Replies
awcodes
awcodes2y ago
Did you try it with a hasManyThrough relationship.? Don’t have much experience with them. But that’s what it sounds like to me.
Gunnolfson
GunnolfsonOP2y ago
It doesn't handle the belongs to many relationship. I'd rather need a belongsToManyThroughMany 😄
awcodes
awcodes2y ago
Sounds horrible.
Gunnolfson
GunnolfsonOP2y ago
I need to display the courses related to the school 😉
Gunnolfson
GunnolfsonOP2y ago
It would be way more convenient than having to open a specific specialism It's deadly simple to have a sql query, but not to plug it to the ❤️ relation manager
awcodes
awcodes2y ago
Have you looked at the Attach actions? Relation managers still have a query that you can modify to use with joins. They’re still just a table from the tables package.
Gunnolfson
GunnolfsonOP2y ago
Oh, that sounds interesting 🤔 I'll look into that. I guess it's unusual thus not documented 😅
Gunnolfson
GunnolfsonOP2y ago
Hmm ok I see the attaching actions. I've found out that there is indeed the getTableQuery method, which is possible to override. However I don't see how it's possible to write it 😅 Is there a specific way to get the query builder as I don't have a relationship? Or should I give the specialisms relationship, serving as a start point? 🤔
awcodes
awcodes2y ago
I’d probably create a pivot table linking the three, but yea you’re going to need a relationship either way, whether that’s a through or with pivot data.
Gunnolfson
GunnolfsonOP2y ago
This morning you inspired me a solution based on a view, adding the school ID on the course, creating a dedicated CourseSchool model just for the table. I guess then I can override the urls for the actions 🤔 (to actually use the Course model) I indeed actually solved it using a view like so:
CREATE OR REPLACE VIEW course_school_view AS
SELECT courses.id as course_id, schools.id as school_id FROM courses
INNER JOIN course_specialism ON courses.id = course_specialism.course_id
INNER JOIN specialisms ON course_specialism.specialism_id = specialisms.id
INNER JOIN schools ON specialisms.school_id = schools.id;
CREATE OR REPLACE VIEW course_school_view AS
SELECT courses.id as course_id, schools.id as school_id FROM courses
INNER JOIN course_specialism ON courses.id = course_specialism.course_id
INNER JOIN specialisms ON course_specialism.specialism_id = specialisms.id
INNER JOIN schools ON specialisms.school_id = schools.id;
This way I can simulate a School::belongsToMany(Course::class, 'course_slug') behavior 👍 I guess I just miss the associate/dissociate features as a view is readonly, but it should be ok for us Thanks for your help 🙏
awcodes
awcodes2y ago
Glad you got it working.
Want results from more Discord servers?
Add your server