Currently I have a one to many relationship setup. The way it works is one Champion has many Skins.
champions
table has id, champion
skins
table has id, set, champion, skin, champion_id
- The champion
field in this table is redundant, but that's only temporary.
The problem I'm getting is that when I call the skins
table to receive champion name, it throws an error.
Models:
class Champion extends Eloquent {
protected $table = 'champions';
public function skin(){
return $this->hasMany('Skin');
}
}
class Skin extends Eloquent {
protected $table = 'skins';
public function champion() {
return $this->belongsTo('Champion');
}
}
Controller:
$champ = Champion::find(2);
foreach ($champ->skin as $c) {
echo $c->set; //outputs the set for id=2
}
$skin = Skin::all();
foreach ($skin as $s) {
echo $s->champion->champion; //errors out.
echo $s->champion; //outputs all champion names (multiple times, if they have multiple skins).
}
What I want to out put is the Champion name, and then all of the skins the champion has. Perhaps someone can help tell me what's wrong and why this isn't working. I've looked at Laravel eloquent - One to many relationships where essentially is the same thing I'm doing, but still does not work.
Example output:
champion1
skin1
skin2
skin3
champion2
skin1
skin2