dimanche 28 février 2016

eloquent ORM (laravel): select from two tables associated with pivot table

I have tree tables in my database (Posts Tags Post_tag(pivot table)):

  1. Posts(id,content,title,...)
  2. Tags(id,tag_name)
  3. post_tag(id,post_id,tag_id)

Now my problem is about how can I select all posts with same tags to an post for example: select all posts that have similar tags as post 1. I have create the relationships between the posts and tags tables: In posts table:

    public function tags(){
    return $this->belongsToMany('App\Tag');}

In tags table:

    public function posts(){
    return $this->belongsToMany('App\Post');}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1php1I6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire