I have a table store, and store has many libraries, in library I have foreign key of store store_id
.
Store table
id(PK)
Library table
id(PK)
store_id(FK)
I'm confused with hasMany
and belongsTo
parameters include, in the docs it says
return $this->hasMany('AppComment', 'foreign_key');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
return $this->belongsTo('AppPost', 'foreign_key', 'other_key');
Which table of hasMany foreign_key and local_key came from? Same with belongsTo which table of foreign_key and other_key came from?
Store model
public function library(){
return $this->hasMany('AppLibrary', 'what_foreign_key_should_be_here','what_other_key_should_be_here');
}
Library model
public function stores(){
return $this->belongsTo('AppStores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here');
}
Because sometimes I change my primary key id of a table to other name like sid, so I always want to specify which is foreign key and primary key
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…