【发布时间】:2018-04-01 03:45:19
【问题描述】:
我有一个表存储,并且存储有很多库,在库中我有存储 store_id 的外键。
存储表
id(PK)
库表
id(PK)
store_id(FK)
我对 hasMany 和 belongsTo 参数感到困惑,在 docs 它说
return $this->hasMany('App\Comment', 'foreign_key');
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
hasMany foreign_key 和 local_key 来自哪个表?和belongsTo一样foreign_key和other_key是从哪个表来的?
店铺模式
public function library(){
return $this->hasMany('App\Library', 'what_foreign_key_should_be_here','what_other_key_should_be_here');
}
库模型
public function stores(){
return $this->belongsTo('App\Stores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here');
}
因为有时候我把一个表的主键id改成其他名字,比如sid,所以我总是想指定哪个是外键和主键
【问题讨论】:
标签: php mysql laravel eloquent