laraveleloquent

Tag a relationship, not a model


In my Laravel app, I have User, List and ListItem models, with a one-to-many relationship between User and List and many-to-many relationships between User and ListItem and between List and ListItem. Each User should be able to tag the ListItems on each List separately, so that ListItem 1 can have different tags on List A and List B. My lists_listitems pivot table has a column "tags" but I don't know how to define a Tag model on a relationship.

I thought about simplifying the problem by creating a Tag model with a many-to-many relationship with ListItems, but as all Users have access to all ListItems in the data base, a ListItem's Tags would then be visible to all Users. And if I would try to make the Tags visible only to one User, I would have the same problem (just for the listitems_user pivot table instead of the lists_listitmes one). I searched the docs and the web but did not get any useful hits, which might be due to Tags being a very common example for a many-to-many relationship, without the additional complication that my app necessitates. Is it even possible to tag relationships in Laravel?


Solution

  • I would make an additional Tag model so the entities would be Tag, User, List, ListItem, with one pivot list_item_user_tags.

    I would leave the users table as it is, then the tags table - id, name, user_id (so the tags are user-specific); lists table - id, name; list_items table - id, name; list_item_user_tags table - id, list_id, list_item_id, tag_id, user_id.