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