mercredi 16 mai 2018

Retrieving rows from a third table that match two pivot tables

I have 5 tables in my Laravel app. 3 main tables are books, places, and tags. The books table has a pivot table to relate it to tags: book_tags. The places table also has a pivot table: place_tags. Now, for each place, I'd like to get the books based on tags that are both in place_tags and book_tags.

books
    book_tags
places
    place_tags
tags

I've looked through the laravel docs and the closest this seems to come to is a many to many polymorphic relationship, but with that, there'd be only one pivot table with a tag_type, which is not what I have.

How would I build this query to retrieve the books for each place based on tags in both pivot tables? Answers in MySQL are cool too, I just don't know how this would look like as a query...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire