問(wèn)題描述
我有一個(gè)表存儲(chǔ),存儲(chǔ)有很多庫(kù),在庫(kù)中我有存儲(chǔ)store_id
的外鍵.
I have a table store, and store has many libraries, in library I have foreign key of store store_id
.
存儲(chǔ)表
id(PK)
圖書(shū)館表
id(PK)
store_id(FK)
我對(duì) hasMany
和 belongsTo
參數(shù)感到困惑,包括在 docs 它說(shuō)
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');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
return $this->hasMany('AppComment', 'foreign_key', 'local_key');
return $this->belongsTo('AppPost', 'foreign_key', 'other_key');
return $this->belongsTo('AppPost', 'foreign_key', 'other_key');
hasMany foreign_key 和 local_key 來(lái)自哪個(gè)表?和belongsTo一樣,foreign_key和other_key是從哪個(gè)表來(lái)的?
Which table of hasMany foreign_key and local_key came from? Same with belongsTo which table of foreign_key and other_key came from?
店鋪模式
public function library(){
return $this->hasMany('AppLibrary', 'what_foreign_key_should_be_here','what_other_key_should_be_here');
}
圖書(shū)館模型
public function stores(){
return $this->belongsTo('AppStores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here');
}
因?yàn)橛袝r(shí)候我把一個(gè)表的主鍵id改成sid這樣的其他名字,所以我總是想指定哪個(gè)是外鍵和主鍵
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
推薦答案
為了簡(jiǎn)化語(yǔ)法,想到了 return $this->hasMany('AppComment', 'foreign_key', 'local_key');
參數(shù)為:
To simplify the syntax, think of the return $this->hasMany('AppComment', 'foreign_key', 'local_key');
parameters as:
- 您要鏈接到的模型
- 鏈接回當(dāng)前表的
id
列的外部表(您要鏈接到的表)的列(除非您指定了第三個(gè)參數(shù),在這種情況下它將使用那個(gè)) - 應(yīng)該使用的當(dāng)前表的列 - 即如果您不希望其他表的外鍵鏈接到當(dāng)前表的
id
列
- The model you want to link to
- The column of the foreign table (the table you are linking to) that links back to the
id
column of the current table (unless you are specifying the third parameter, in which case it will use that) - The column of the current table that should be used - i.e if you don't want the foreign key of the other table to link to the
id
column of the current table
在您的情況下,因?yàn)槟?libraries
表中使用了 store_id
,您的生活變得輕松了.在您的 Store
模型中定義時(shí),以下內(nèi)容應(yīng)該可以完美運(yùn)行:
In your circumstance, because you have used store_id
in the libraries
table, you've made life easy for yourself. The below should work perfectly when defined in your Store
model:
public function libraries()
{
return $this->hasMany('AppLibrary');
}
在幕后,Laravel 會(huì)自動(dòng)將Store
表的id
列鏈接到Library<的
store_id
列/code> 表格.
Behind the scenes, Laravel will automatically link the id
column of the Store
table to the store_id
column of the Library
table.
如果你想明確定義它,那么你可以這樣做:
If you wanted to explicitly define it, then you would do it like this:
public function libraries(){
return $this->hasMany('AppLibrary', 'store_id','id');
}
- 模型標(biāo)準(zhǔn)是單數(shù)命名的函數(shù)返回一個(gè)belongsTo,而復(fù)數(shù)函數(shù)返回一個(gè)hasMany(即
$store->libraries() 或$library->store()
).
這篇關(guān)于Laravel hasMany 和belongsTo 參數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!