【问题标题】:Laravel Many to Many Relationship - Linking 3 itemsLaravel 多对多关系 - 链接 3 个项目
【发布时间】:2014-04-21 23:12:13
【问题描述】:

我有一个users

|------------|
|   USERS    |
|------------|
|id          |
|f_name      |
|l_name      |
|------------|

现在这些用户可以相互关联 - 爸爸、兄弟、姐妹等。

为此,我有一个relations

|------------|
|  RELATIONS |
|------------|
|id          |
|type        |
|------------|

现在我如何关联用户?我试过的是: 我创建了一个relation_user

|---------------|
| RELATION_USER |
|---------------|
|id             |
|user_id        |
|user2_id       |
|relation_id    |
|---------------|

型号:

Relation.php

public function user()
{
    return $this->belongsToMany('User');
}

User.php

public function relations()
{
    return $this->belongsToMany('Relation');
}

我尝试像这样抓住关系:

$u = User::find('11');
var_dump($u->relations->toArray());

这是我得到的:

array(1) {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["relation"]=>
    string(12) "Grand Father"
    ["pivot"]=>
    array(2) {
      ["user_id"]=>
      string(2) "11"
      ["relation_id"]=>
      string(1) "1"
    }
  }
}

请注意,它缺少数据透视表 (relation_user) 中的 user2_id。我怎样才能获得第三场。这也是正确的数据库设计吗?谢谢。

我知道我可以执行以下代码。只想知道最佳实践/更好的方法。可能是可以利用eager loading 并返回Laravel collection 的东西?

$a = DB::table('relation_user')->where('user2_id', '=', '1')->get(array('user_id', 'user2_id', 'relation_id'));

【问题讨论】:

  • 您能否详细说明您是如何编写模型类的?
  • @har2vey 请检查更新后的问题

标签: database database-design laravel-4


【解决方案1】:

好的,我的问题得到了很好的解决方案。这是我所做的:

型号:

Relation.php

public function user(){
    return $this->belongsToMany('User', 'relation_user', 'user_id', 'relation_id')->withPivot('user2_id')->withTimestamps();
}

User.php

public function relations() {
    return $this->belongsToMany('Relation', 'relation_user', 'user_id', 'relation_id')->withPivot('user2_id')->withTimestamps();
}

像这样抓住关系:

$user = User::find('11');
$user->relations->toArray();

给我以下输出:

array(1) {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["relation"]=>
    string(12) "Grand Father"
    ["created_at"]=>
    string(19) "2014-03-17 01:18:54"
    ["updated_at"]=>
    string(19) "2014-03-17 01:18:54"
    ["pivot"]=>
    array(5) {
      ["user_id"]=>
      string(2) "11"
      ["relation_id"]=>
      string(1) "1"
      ["user2_id"]=>
      string(1) "1"
      ["created_at"]=>
      string(19) "2014-03-17 02:02:43"
      ["updated_at"]=>
      string(19) "2014-03-17 02:02:43"
    }
  }
}

然后像这样插入:

$user->relations()->attach(1, array('user2_id' => 2));

【讨论】:

    【解决方案2】:

    您需要像这样更正您的模型类:

    public function user()
    {
        return $this->belongsTo('User'); //one relation entry correspond to one user
    }
    
    public function relations()
    {
        return $this->hasMany('Relation'); //one user has multiple relations
    }
    
    //query the user and the relations
    $u = User::find(11)->relations; // this will returns entries in relation table with user_id=11
    

    【讨论】:

    • 关系不属于用户。我正在使用数据透视表来实现标准化。
    • 看起来我弄错了您的架构设计,在您的情况下,relation_user 表应该重命名关系,而关系表应该重命名关系描述。因为当前的关系表只是存储了关系的描述,所以实际上只是用户表加入了当前的relation_user进行查询,而当前的关系表只提供了基于relation_id的关系的文字描述。
    猜你喜欢
    • 1970-01-01
    • 2022-10-21
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2013-01-22
    • 2015-06-06
    • 2021-05-25
    相关资源
    最近更新 更多