0

In Laravel can I create a relationship between 2 ids in the same table? In my case the hamming distance between 2 photos. I want to save the hamming_distance of every id to every other id. One for each relationship. No Dups. Not worried about scale or how long it takes to generate each hamming_distance. Really just looking for the best schema to handle this.

Photos Table

id
path
etc

Another table. Or whatever is suggested as 2 columns of the same name obviously will not work

photo_id
photo_id
hamming_distance
BobB
  • 750
  • 1
  • 9
  • 22
  • would this be a possible [duplicate](https://stackoverflow.com/questions/29751859/laravel-5-hasmany-relationship-on-two-columns) – Hamza Mohamed Mar 07 '18 at 04:27
  • It is close. So a schema of photo_id and related_photo_id is a reasonable start. My query would need to be something like "Where photo_id = 17 and related_photo_id = 18 OR photo_id = 18 and related_photo_id = 17". I imagine there is a better way – BobB Mar 07 '18 at 16:30

1 Answers1

0

In Your 2nd Table

public function Photo(){

   return $this->belongsTo('App\Photo', 'photo_id', 'id');
}

For More Details https://laravel.com/docs/5.6/eloquent-relationships#one-to-one

Gowthaman D
  • 574
  • 7
  • 19
  • Could you show me the schema of the second table that would make this work. This is not how I understand One to One. Every photo has a relationship to every other photo. – BobB Mar 07 '18 at 16:19