#TIL : Eloquent Many-to-Many Relationship


02 Dec 2015 / by KhanhIceTea

To create the n-to-m relationship in Eloquent, we create a table stand between 2 tables.
Eg:

We have the db schema :

postpost_tagtag
id (PK)id (PK)id (PK)
titlepost_id (Index)name (Unique)
contenttag_id (Index)

In the model Post and Tag, we define a relation :

  • App\Model\Post.php
public function tags() {
    return $this->belongsToMany('App\Model\Tag', 'post_tag', 'post_id', 'tag_id');
}
  • App\Model\Tag.php
public function posts() {
    return $this->belongsToMany('App\Model\Post', 'post_tag', 'tag_id', 'post_id');
}

After that, we can use the relation to fetch the related data, like

$post = Post::firstOrFail(1);
$tags = $post->tags; // return the Collections class contains tags

or

$tag = Tag::where('name', '=', 'php')->first();
$posts_of_tag = $tag->posts; // return the Collections class contains posts

When updating or creating post, we have to sync with tags by

$tag_ids = [2, 3 ,4]; // Get tag ids from tag names
$post->tags()->sync($tag_ids);

Sound good ?