duangu6431 2014-05-05 03:32
浏览 39
已采纳

Laravel 4 Eloquent Relationship无法正常工作

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
  • 写回答

3条回答 默认 最新

  • dongxiane0395 2014-05-05 07:53
    关注

    Your relations are correct, the controller has flaws.

    Problem is that you have a field called champion and the relation has the same name

    The only thing I would change is relation name skin -> skins to make it verbose.

    To make what you need do this:

    // Champion model
    public function skins()
    {
       return $this->hasMany('Skin');
    }
    
    // controller
    $champions = Champion::with('skins')->get(); // with() means eager loading to avoid n+1 issue
    
    @foreach($champions as $champion)
       {{ $champion->id }}
       @foreach($champion->skins as $skin)
          <div> {{ $skin->set }} </div> // or whatever you like here
       @endforeach
    @endforeach
    

    Now the controller that doesn't work:

    $skin = Skin::all();
    foreach ($skin as $s) {
    
        echo $s->champion->champion; // $s->champion is a field on skins table    
        echo $s->champion; // outputs this field value
    }
    

    In this situation you can do this (but you'd rather not, rename field or relation instead):

    echo $s->champion()->first()->id; // fetch related model and output its property
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口