Laravel:如何使用多个数据透视表关系 [英] Laravel: How to use multiple pivot table relationships

查看:440
本文介绍了Laravel:如何使用多个数据透视表关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新定义关系和框架在一起,我只是习惯了原始的sql。



Heres是相关信息:
用户表:

/ p>

 表:用户
id - int(自动递增)
username - varchar

挑战表:

 表:挑战
id - int(auto increment)
name - varchar

User_challenge_links

 表User_challenge_links 
id - int(自动递增)
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 -
pre>

所以我的目标.. user->挑战。

  class User extends Eloquent 
{
protected $ table =users;

public function challenges(){
// get user->挑战
}
}

关系:




  • 用户有许多User_challenge_links

  • User_challenge_link有一个challenge_sub_categories_id(因此是一个challenge_sub_category)

  • Challenge_sub_categories的challenge_id符合挑战表中的挑战



任何帮助,甚至指向正确的方向, !



编辑
示例数据:
用户数据

 用户
id name
1Sjaak
2Henk

类别资料
id name
1Foo
2Bar



子类别资料
id name
1SubFoo
2SubBar



挑战资料
ID名称
1Swing dat Foo
2Bar all the things
3Foo The Bars



Challenge_sub_categories Data



  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

数据流:

用户可以创建类别或使用现有类别并将挑战链接到现有或新的类别。但是,用户还可以选择使用子类别,然后他将链接到类别,并将挑战链接到该类别。

因此,类别是必需的,但子类别不是。然而,如果使用子类别(再次存在或新的),挑战将被连接到该子类别。



注意:子类别可连接到多个类别

 类别 - 房子
子类别 - 清洁
挑战 - 获得特殊肥皂

类别 - 汽车
子类别 - 清洁
挑战 - 获取特殊肥皂

类别 - 洗澡
挑战 - 获取特殊肥皂

这是一些可能的情况

解决方案

我尝试保持命名尽可能简单。

 用户
id
用户名

challenge_user
user_id
challenge_id

挑战
id
name
topic_id
category_id

主题
id
名称

类别
id
名称

定义你的口才模型

  class User extends Eloquent {
public function challenges(){
return $ this-> belongsToMany('Challenge');
}
}

类挑战扩展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类别extends Eloquent {
public function challenges(){
return $ this-> hasMany('Challenge');
}
}

使用你的Eloquent模型...只是一些例子你可以做什么。

  //主题名称的所有挑战的集合
主题:: with ) - > whereName($ topic_name) - > first() - > challenges;

//按类别名称收集所有挑战
类别:: with('chall') - > whereName($ category_name) - > first() - &

//通过Challenge id
收集所有用户Challenge :: with('users') - > find($ challenge_id) - > users;

//至少有2个挑战的用户集合
User :: has('challenges','>',1) - > get

//将挑战附加到用户
$ user = User :: find($ id);
$ user-> challenges() - > attach($ challenge_id);

//为挑战分配主题
$ challenge = Challenge :: find($ challenge_id);
$ topic = Topic :: find($ topic_id);

$ challenge-> topic() - > associate($ topic);
$ challenge-> save();

参考和建议阅读:



Laravel雄辩的关系 belongsTo belongsToMany hasMany



查询关系 Model :: has()



热切加载 Model :: with()



访问关系的动态属性解决 $ model->关系



插入相关模型 attach() associate()



查询范围



使用数据透视表如果需要从数据透视表中检索其他数据。


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.

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

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      -

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

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"

Sub_categories Data id name 1 "SubFoo" 2 "SubBar"

Challenges Data id name 1 "Swing dat Foo" 2 "Bar all the things" 3 "Foo The Bars"

Challenge_sub_categories Data

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

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 sub category, 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 sub category.

Note: A sub category 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

解决方案

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

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.

// 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 Relationships belongsTo belongsToMany hasMany

Querying relations Model::has()

Eager Loading Model::with()

Dynamic Properties for Accessing Relations Resolve $model->relationship

Inserting related Models attach() associate()

Query Scopes

Working with Pivot tables If you need to retrieve extra data from the pivot table.

这篇关于Laravel:如何使用多个数据透视表关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆