私はテーブルストアを持っており、ライブラリにはライブラリがたくさんあります。ライブラリにはstore_id
という外部キーがあります。Laravel hasManyおよびbelongsToパラメータ
ストアテーブル
id(PK)
ライブラリテーブル
id(PK)
store_id(FK)
私は、hasMany
とbelongsTo
パラメータで混乱している、docsでは
リターンの$ this - > hasManyの言います( 'App \ Comment'、 'foreign_key');
返す$ this-> hasMany( 'App \ Comment'、 'foreign_key'、 'local_key');
返す$ this-> belongsTo( 'App \ Post'、 'foreign_key'、 'other_key');
hasMany foreign_keyとlocal_keyのテーブルはどれですか? foreign_keyとother_keyのテーブルが由来するbelongsToと同じですか?
ストアモデル
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');
}
時々私は、SIDのような他の名前にテーブルの私の主キーIDを変更するので、私は常に外部キーであるかを特定したいのでと主キー