Reputation: 33
User TABLE: id -> int email -> varchar
Tag TABLE: id -> int name -> varchar
tags_user TABLE tab_id ->foreign('tag_id')->references('id')->on('tag'); user_id ->foreign('user_id')->references('id')->on('user');
how to get all tags from specific user?
thanks
Upvotes: 0
Views: 75
Reputation: 12169
You need many to many relationship:
In your user model:
class User extends Eloquent {
public function tags()
{
return $this->belongsToMany('Tag');
}
}
In your Tag model:
class Tag extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Then from your controller: Utilize the many to many relationship.
$tags = User::find(1)->tags;
Important:
Three database tables are needed for this relationship: users
, tags
, and tag_user
. The tag_user
table is derived from the alphabetical order of the related model names, and should have user_id
and tag_id
columns.
Reference:
http://laravel.com/docs/eloquent#many-to-many
Upvotes: 2