我有我的數據庫中的以下結構的Laravel 4透視表:實現使用雄辯
-shared_resources表 標籤都有效表 -shared_resource_tag表
有一個許多人shared_resources之間一對多的關係和標籤。當我創建一個shared_resource我做到以下幾點:
- 的shared_resource添加到shared_resources表
- 標籤添加到標籤表
- 添加shared_resource_id和TAG_ID到shared_resource_tag表
我可以設法通過第1步和第2步,但出於某種原因,我無法對pivot table
進行任何輸入。我不知道爲什麼。我在models
建立的關係相應:
SharedResource
:
class SharedResource extends Eloquent{
public function tags(){
return $this->belongsToMany('Tag');
}
}
Tag
:
class Tag extends Eloquent{
public function sharedResources(){
return $this->belongsToMany('SharedResource');
}
}
然後當我創造我這樣做的條目:
$tags = Array();
$tags = explode(',', Input::get('tags'));
foreach($tags as $tag){
$newTag = new Tag;
$newTag->name = $tag;
$newTag->save();
}
//Pivot table entry
$resource->tags()->sync($tags);
$resource->save();
上面的代碼出現錯誤:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'shared_resource_id' in 'where clause' (SQL: select `tag_id` from `shared_resource_tag` where `shared_resource_id` is null)
我來怎麼回事很迷茫,我明白了Eloquent
做出實施這些N:N的關係容易。
下獲取更多here不是真的,否則我不認爲我會採取了鍵入問題的時間...... – Javacadabra 2014-08-27 16:44:48
那麼你在那張桌子上有'shared_resource_id'字段..? – 2014-08-27 16:49:34