雄辩不连接表格

时间:2017-12-06 19:57:06

标签: php mysql laravel eloquent

所以我有3张桌子。但Laravel未能将桌子连接到彼此,我觉得我错过了一个非常小的东西,但我似乎无法找到它。

控制器

$data = Item::all();
$clipper = $data->first();
dd($clipper->attributes());

物品

protected $table = 'item';
protected $primaryKey = 'item_id';
public $timestamps = false;

public function attributes() {
    return $this->hasMany('App\Attributes', 'item_id');
}

Atrributes

protected $table = 'attributes';
protected $primaryKey = 'attr_id';
public $timestamps = false;

public function name() {
    return $this->hasOne('App\Attributesname', 'name_id');
}

属性名称

protected $table = 'item_attributes';
protected $primaryKey = 'item_attr_id';
public $timestamps = false;

dd的结果

Result of dd

数据库设计

Design of my database

1 个答案:

答案 0 :(得分:1)

Laravel链接关系很好,看你输出父模型是Item,相关模型是Atribute,试试这个看相关数据(不是关系):

$data = Item::all();
$clipper = $data->first();
dd($clipper->attributes);
相关问题