Laravel:如何使用多个数据透视表关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23336145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to use multiple pivot table relationships
提问by Pwnball
I'm new to defining relationships and frameworks all together, I'm just used to raw SQL. Did my homework (google + Laravel documentation) but I think I'm just not understanding it properly.
我是一起定义关系和框架的新手,我只是习惯了原始 SQL。做了我的作业(google + Laravel 文档),但我想我只是没有正确理解它。
Heres is the relevant information: User Table:
以下是相关信息: 用户表:
Table: Users
id - int (auto increment)
username - varchar
Challenges Table:
挑战表:
Table: Challenges
id - int (auto increment)
name - varchar
User_challenge_links
User_challenge_links
Table User_challenge_links
id - int (auto increment)
user_id - int
challenge_sub_categories_id - int
Challenge_sub_categories
Challenge_sub_categories
Table Challenge_sub_categories
id - int (auto increment)
category_id -
sub_category_id -
challenge_id -
So my goal.. user->challenges.
所以我的目标.. 用户-> 挑战。
class User extends Eloquent
{
protected $table = "users";
public function challenges() {
// get user-> challenges
}
}
The relationships:
关系:
- A user has many User_challenge_links
- A User_challenge_link has a challenge_sub_categories_id (thus a challenge_sub_category)
- A challenge_id from challenges_sub_categories matches a challenge in the challenges table
- 一个用户有很多 User_challenge_links
- User_challenge_link 有一个challenge_sub_categories_id(因此是challenge_sub_category)
- 来自challenge_sub_categories 的challenge_id 匹配挑战表中的挑战
Any help, even pointing me in the right direction will be much appreciated!
任何帮助,甚至将我指向正确的方向,将不胜感激!
Edit:example data: Users Data
编辑:示例数据:用户数据
Users
id name
1 "Sjaak"
2 "Henk"
Categories Data id name 1 "Foo" 2 "Bar"
类别 数据 id 名称 1 "Foo" 2 "Bar"
Sub_categories Data id name 1 "SubFoo" 2 "SubBar"
Sub_categories 数据 id 名称 1 "SubFoo" 2 "SubBar"
Challenges Data id name 1 "Swing dat Foo" 2 "Bar all the things" 3 "Foo The Bars"
挑战 数据 ID 名称 1 “Swing dat Foo” 2 “Bar all the things” 3 “Foo The Bars”
Challenge_sub_categories Data
Challenge_sub_categories 数据
id category_id sub_category_id challenge_id
1 1 1 1
2 2 1 1
3 1 2 2
4 2 1 3
5 2 2 2
User_challenge_links Data
User_challenge_links 数据
id user_id Challenge_sub_categories_id
1 1 1
2 1 3
3 2 2
4 2 3
5 2 4
Dataflow:
A user can create categories or use existing ones and link challenges to them (existing or new). However, a user can also choose to use a subcategory, which he then links to a category and link challenges to that instead.
So, a category is mandatory, but a sub_category isn't. If however a sub_category is used (again.. existing or new) the challenge will be connected to that subcategory.
数据流:
用户可以创建类别或使用现有类别并将挑战链接到它们(现有的或新的)。但是,用户也可以选择使用子类别,然后将其链接到一个类别并将挑战链接到该类别。
因此,类别是强制性的,但 sub_category 不是。然而,如果使用 sub_category(再次......现有的或新的),挑战将连接到该子类别。
Note:A subcategory CAN be connected to multiple categories
注意:一个子类别可以连接到多个类别
category - House
sub_category - Cleaning
Challenge - getting special soap
category - Car
sub_category - Cleaning
Challenge - getting special soap
category - Showering
Challenge - getting special soap
These are some possible situations
这些是一些可能的情况
回答by lagbox
This setup should get you going. I tried to keep the naming as simple as possible.
这个设置应该让你开始。我试图保持命名尽可能简单。
users
id
username
challenge_user
user_id
challenge_id
challenges
id
name
topic_id
category_id
topics
id
name
categories
id
name
Defining your Eloquent Models
定义你的 Eloquent 模型
class User extends Eloquent {
public function challenges() {
return $this->belongsToMany('Challenge');
}
}
class Challenge extends Eloquent {
public function users() {
return $this->belongsToMany('User');
}
public function topic() {
return $this->belongsTo('Topic');
}
public function category() {
return $this->belongsTo('Category');
}
}
class Topic extends Eloquent {
public function challenges() {
return $this->hasMany('Challenge');
}
}
class Category extends Eloquent {
public function challenges() {
return $this->hasMany('Challenge');
}
}
Using your Eloquent Models ... just some exmaples of what you can do.
使用你的 Eloquent 模型......只是你可以做的一些例子。
// Collection of all Challenges by Topic name
Topic::with('challenges')->whereName($topic_name)->first()->challenges;
// Collection of all Challenges by Category name
Category::with('challenges')->whereName($category_name)->first()->challenges;
// Collection of all Users by Challenge id
Challenge::with('users')->find($challenge_id)->users;
// Collection of Users with atleast 2 Challenges
User::has('challenges', '>', 1)->get();
// Attach Challenge to User
$user = User::find($id);
$user->challenges()->attach($challenge_id);
// Assign a Topic to a Challenge
$challenge = Challenge::find($challenge_id);
$topic = Topic::find($topic_id);
$challenge->topic()->associate($topic);
$challenge->save();
References and suggested reading:
参考资料和建议阅读:
Laravel Eloquent RelationshipsbelongsTo
belongsToMany
hasMany
Laravel 雄辩的关系belongsTo
belongsToMany
hasMany
Querying relationsModel::has()
查询关系Model::has()
Eager LoadingModel::with()
急切加载Model::with()
Dynamic Properties for Accessing RelationsResolve $model->relationship
访问关系解析的动态属性$model->relationship
Inserting related Modelsattach()
associate()
插入相关模型attach()
associate()
Working with Pivot tablesIf you need to retrieve extra data from the pivot table.