我是新手一起定义关系和框架,我只是习惯于原始SQL。我的作业(谷歌+ Laravel文档),但我想我只是没有正确理解它。
Heres是相关信息: 用户表:
Table: Users
id - int (auto increment)
username - varchar
挑战表:
Table: Challenges
id - int (auto increment)
name - varchar
User_challenge_links
Table User_challenge_links
id - int (auto increment)
user_id - int
challenge_sub_categories_id - int
Challenge_sub_categories
Table Challenge_sub_categories
id - int (auto increment)
category_id -
sub_category_id -
challenge_id -
所以我的目标......用户 - >挑战。
class User extends Eloquent
{
protected $table = "users";
public function challenges() {
// get user-> challenges
}
}
关系:
任何帮助,甚至指向正确的方向都将非常感激!
修改 示例数据: 用户数据
Users
id name
1 "Sjaak"
2 "Henk"
分类数据 id名称 1“Foo” 2“酒吧”
Sub_categories数据 id名称 1“SubFoo” 2“SubBar”
挑战数据 id名称 1“Swing dat Foo” 2“禁止所有的东西” 3“Foo The Bars”
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数据
id user_id Challenge_sub_categories_id
1 1 1
2 1 3
3 2 2
4 2 3
5 2 4
数据流:
用户可以创建类别或使用现有类别并将挑战链接到它们(现有的或新的)。但是,用户也可以选择使用子类别,然后将其链接到类别并将挑战链接到该类别
因此,类别是必需的,但sub_category不是。但是,如果使用了sub_category(再次是..现有的或新的),则挑战将连接到该子类别。
注意:子类别可以连接到多个类别
category - House
sub_category - Cleaning
Challenge - getting special soap
category - Car
sub_category - Cleaning
Challenge - getting special soap
category - Showering
Challenge - getting special soap
这些是一些可能的情况
答案 0 :(得分:19)
此设置应该让你前进。我试图让命名尽可能简单。
users
id
username
challenge_user
user_id
challenge_id
challenges
id
name
topic_id
category_id
topics
id
name
categories
id
name
定义您的雄辩模型
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');
}
}
使用您的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();
参考资料和建议阅读:
Laravel Eloquent Relationships belongsTo
belongsToMany
hasMany
Querying relations Model::has()
Eager Loading Model::with()
Dynamic Properties for Accessing Relations解析$model->relationship
Inserting related Models attach()
associate()
Working with Pivot tables如果您需要从数据透视表中检索额外数据。