I'm trying to put together a wrestling project and have arrived to the point where I need to put together an event card. For every event there is an event name, date for the event, arena and matches for the event.
Each match has a match number, match type.
I also have a table for all roster members which has a fields for id and roster_name.
The list of competitors for the match is listed in another table called event_matches_appearances and has fields for match_id, competitor_id.
To explain this. If Wrestler A and Wrestler B faced off against each other in the first match of event 1 then the tables would look like the following.
Table: roster
id roster_name
1 Wrestler A
2 Wrestler B
Table: event_matches_appearances
id match_id competitor_id team
1 1 1 0
2 1 2 0
I have the appearances returned but now I am trying to foreach this data and then having it make a relationship to get the roster names of those competitors and its not working.
@foreach($match->competitors AS $competitor)
@endforeach
Table: event_matches
id eventID matchNumber matchTypeID
1 1 1 1
<?php
class Match extends \Eloquent {
// LINK THIS MODEL TO OUR DATABASE TABLE ---------------------------------
protected $table = 'events_matches';
// DEFINE RELATIONSHIPS --------------------------------------------------
public function event()
{
return $this->belongsTo('Events');
}
public function type()
{
return $this->belongsTo('MatchType', 'match_type_id');
}
public function stipulation()
{
return $this->belongsTo('MatchStipulation', 'match_stipulation_id');
}
public function title()
{
return $this->belongsTo('Title');
}
public function competitors()
{
return $this->hasMany('EventMatchesAppearances');
}
}
I don't understand the problem. You want to get the roster name of the competitor ? Isn't it just a relationship between roster and competitor ?
Yes however I'm trying to figure out how to get that relationship to show up when I need to echo the roster name of the competitor.
How should I be getting the roster name?
foreach($match->competitors as $competitor){
echo $competitor->roster->roster_name;
}
Assuming that
class Competitor extends Eloquent{
public function roster(){
return $this->belongsTo('Roster');
}
}
I have the following actually and I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roster.event_matches_appearances_id' in 'where clause' (SQL: select * from `roster` where `roster`.`event_matches_appearances_id` = 1)
I'm trying to figure out what I need to do so that it will get the correct roster_name correctly.
<?php
class EventMatchesAppearances extends Eloquent {
// LINK THIS MODEL TO OUR DATABASE TABLE ---------------------------------
protected $table = 'event_matches_appearances';
// DEFINE RELATIONSHIPS --------------------------------------------------
public function roster()
{
return $this->hasMany('Roster');
}
}
You can override the default foreign key names when defining the relationship. Check the doc on eloquent relationships.
I get the correct query run to get the wrestler however when I try and echo his name it does not work. It errors saying undefined property::$roster_name.
@foreach($match->competitors AS $competitor)
{{ $competitor->roster->roster_name }}
@endforeach
I did some further research and found I have to do an additional foreach statement and loop over it. The solution is as follows.
@foreach ($match->competitors AS $competitor)
@foreach ($competitor->roster AS $member)
{{ $member->roster_name }}
@endforeach
@endforeach
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community