Reputation: 3215
I have 3 tables:
posts
=> id
, title
, body
, ...tags
=> id
, name
, count
post_tag
=> post_id
, tag_id
Post
model has
public function tags() {
return $this->belongsToMany('Tag');
}
Tag
model has
public function posts() {
return $this->belongsToMany('Post');
}
Selecting is working, but I want insert to DB
tags
post_tag
count
For now I have
$post = new Post;
$post->title = Input::post('title');
$post->body = Input::post('body');
$post->save();
I have tags separated with ,
. For example javascript, jquery, ajax
.
How to do it? Explode tags and then check every tag if exist and then do 2 inserts (to tags
and post_tag
) or is there "magic" solution?
Upvotes: 0
Views: 1149
Reputation: 1084
Have a look to this, it'll probably help you
I think the function you need is attach
. I've never used but I believe it's what you need. Have a look ;)
Upvotes: 1