2013-07-05 32 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。我从来没有用过,但我相信这是你需要的。看看;)