I have two tables: players and battles. When a player dwells another one, a new row is inserted on the battles table where the id for challenger player is stored on player1_id column and the challenged player is stored on player2_id
My battles table looks like so:
- id
- player1_id (challenger player)
- player2_id (challenged player)
- ...
What I need is to have a hasMany relationship between players to battles matching either of the columns.
I found a solution for an almost identical issue and tried what user Colling James suggested https://stackoverflow.com/a/29775608/1481559 but it didn't work for me.
Another solution I tried was using union as per Acasar's suggestion on https://laracasts.com/discuss/channels/eloquent/combining-multiple-hasmany-relationships-into-one-model-relation-query-builder
public function battles() {
return $this->hasMany('App\Battle')->union($this->hasMany('App\Battle')->toBase());
}
but I had no success.