$this->hasMany(Match::class, 'team1_id')->orWhere('matches.id', 'team2_id');
Multiple foreign id, Team-Matches
Hi, so I have a table with 'matches' where are team1_id, team2_id, result. Both team1_id and team2_id have foreign key to id from 'teams' table.
In match model I have: team1 and team2 methods are for using values from their database and not just displaying id.
public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}
public function team1(): BelongsTo
{
return $this->belongsTo(Team::class, 'team1_id');
}
public function team2(): BelongsTo
{
return $this->belongsTo(Team::class, 'team2_id');
}
In team model I have:
public function matches(): HasMany
{
return $this->hasMany(Match::class, 'team1_id');
}
Controller is: return view('team', [ 'matches' => $team->matches ]);
So everything works fine except when using in Team model return $this->hasMany(Match::class); This shows error as there is not team_id in table return $this->hasMany(Match::class, 'team1_id'); If I use this one, on page of team it shows me Matches where team is team1_id. And on page of another team where id = team2_id would be blank
Is there a way to make it something like return $this->hasMany(Match::class, 'team1_id' OR 'team2_id');
Please or to participate in this conversation.