我有四个表:characters,pets,characters_pets和characters_pets_metadata。 一个角色在数据透视表characters_pets上有很多宠物,但我还需要从元数据表中获取数据:
class Character extends Model {
protected $table = 'characters';
public function pets() {
return $this->belongsToMany('Pet', 'characters_pets');
}
}
class CharacterPet extends Model {
protected $table = 'characters_pets';
public function metadata() {
return $this->hasMany('PetMetadata');
}
}
如何通过关系获取每个角色宠物的元数据?
答案 0 :(得分:0)
我相信你在找类似的东西:
$character = Character::find(1);
foreach($character->pets as $pet){
// you can access any pet information here
foreach($pet->metadata as $meta){
var_dump($meta); // your meta information here
}
}