0
也许有了这张图片,我可以让自己清楚。 我有user table
和owned_items table
所拥有的项目是使用这个雄辩的方法通过数据透视表检索单表中引用的两个外表中包含的数据
$owned_items = Auth::user()->owned_items->toArray();
我所得到的“item_type_id”连接到用户通过users_owned_item
透视表
和“item_color_id“包含在owned_items
表中。 我真正需要的是item_types
和item_colors
表中包含的实际数据,问题是:如何获取该数据而不是外键标识?
p.s. user_owned_items表中的“item_id”的varchar类型只是一个错误,当然这是一个INT。
也请让我知道如果这些关系是正确的:
在 用户模式public function owned_items(){
return $this->belongsToMany('App\owned_items')->withTimestamps();
}
在
owned_items
模型
public function user(){
return $this->belongsToMany('App\User');
}
public function Item_type(){
return $this->hasOne('App\Item_type');
}
public function Item_color(){
return $this->hasOne('App\Item_color');
}
在ITEM_TYPE
model
public function Owned_item(){
return $this->belongsToMany('App\Owned_item');
}
在Item_color模型
public function Owned_item(){
return $this->belongsToMany('App\Owned_item');
}