0
我有3個表:Laravel 4 - 保存標籤
posts
=>id
,title
,body
...tags
=>id
,name
,count
post_tag
=>post_id
,tag_id
Post
模型
public function tags() {
return $this->belongsToMany('Tag');
}
Tag
模型
public function posts() {
return $this->belongsToMany('Post');
}
選擇工作,但我想插入到DB
- 如果新的標籤 - 插入到
tags
- 插入關係
post_tag
- Incre ASE標籤
count
現在我有
$post = new Post;
$post->title = Input::post('title');
$post->body = Input::post('body');
$post->save();
我有,
分開標籤。例如javascript, jquery, ajax
。
怎麼辦?爆炸標籤,然後檢查每個標籤是否存在,然後做2個插入(到tags
和post_tag
)或者是否有「魔術」解決方案?