Pivot two objects of same table in laravel

Viewed 40

Assume the following two tables (stripped down to this question, assume there is more data on both):

user

  • id

user_similarity

  • user_1_id
  • user_2_id
  • similarity

I'd like to use user_similarity as a pivot table to construct a list of users that are similar to a given user.

To avoid storing duplicate data on the pivot table, I'd only like one record per user pair, with the lower user ID being stored in the first, and the higher ID in the second column.

Is it possible to set this up without duplicate data records and only a single eloquent relation?

0 Answers
Related