2013-07-05 38 views
0

我有3個表:Laravel 4 - 保存標籤

  • posts =>idtitlebody ...
  • tags =>idnamecount
  • post_tag =>post_idtag_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個插入(到tagspost_tag)或者是否有「魔術」解決方案?

回答

1

,看一下這個,它可能會幫助你

Inserting related models

我想你需要的功能是attach。我從來沒有用過,但我相信這是你需要的。看看;)