Shaddow
Shaddow

Reputation: 3215

Laravel 4 - save tags

I have 3 tables:

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

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

Answers (1)

Israel Ortuño
Israel Ortuño

Reputation: 1084

Have a look to this, it'll probably help you

Inserting related models

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

Related Questions