laravel 如何在 Eloquent Orm 中实现自引用 (parent_id) 模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20292329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:43:14  来源:igfitidea点击:

How to implement a self referencing (parent_id) model in Eloquent Orm

phplaraveleloquent

提问by Dinesh Copoosamy

I have a Usertable and need to allow for users to have a parent user.

我有一张User表,需要允许用户拥有父用户。

the table would have the fields:

该表将包含以下字段:

  • id
  • parent_id
  • email
  • password
  • id
  • parent_id
  • email
  • password

How would I define this self referencing relationship in Eloquent ORM?

我将如何在 Eloquent ORM 中定义这种自引用关系?

回答by msturdy

I had some success like this, using your exact DB table.

我使用您的确切数据库表取得了这样的成功。

User Model:

用户模型

class User extends Eloquent {

    protected $table = 'users';
    public $timestamps = false;

    public function parent()
    {
        return $this->belongsTo('User', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('User', 'parent_id');
    }

}

and then I could use it in my code like this:

然后我可以像这样在我的代码中使用它:

$user     = User::find($id);

$parent   = $user->parent()->first();
$children = $user->children()->get();

Give that a try and let me know how you get on!

试试看,让我知道你的进展如何!

回答by Alexander Taubenkorb

I had a chain of self referencing contracts (a contract can be continued by another contract) and also needed self referencing. Each contract has zero or one previous and also zero or one next contract.

我有一系列自引用合同(一个合同可以由另一个合同继续)并且也需要自引用。每个合约都有零个或一个前一个合约,还有零个或一个下一个合约。

My data table looked like the following:

我的数据表如下所示:

+------------------+  
| contracts        |  
+------------------+  
| id               |  
| next_contract_id |  
+------------------+  

To define the inverse of a relationship (previous contract) you have to inverse the related columns, that means setting * foreign key column on the model table * associated column on the parent table (which is the same table)

要定义关系(以前的合同)的反转,您必须反转相关的列,这意味着设置 * 模型表上的外键列 * 父表上的关联列(这是同一个表)

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Contract extends Model {

    // The contract this contract followed
    function previousContract()
    {
        // switching id and next_contract_id
        return $this->belongsTo('App\Contract', 'id', 'next_contract_id');
    }

    // The contract that followed this contract
    function nextContract()
    {
        return $this->belongsTo('App\Contract');
        // this is the same as
        // return $this->belongsTo('App\Contract', 'next_contract_id', 'id');
    }
}

See http://laravel.com/docs/5.0/eloquent#one-to-onefor further details.

有关更多详细信息,请参阅http://laravel.com/docs/5.0/eloquent#one-to-one