簡體   English   中英

Laravel表通過樞軸引用自己

[英]Laravel table referencing itself through pivot

我有一張桌子叫角色:

Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->string('image')->default("noImage.png");
        $table->timestamps();
      });

和另一個表role_roles

Schema::create('roles_roles', function (Blueprint $table) {
        $table->increments('id');
        $table->integer("role_id")->unsigned();
        $table->integer("role_inherit_from_id")->unsigned();
        $table->timestamps();
    });

我希望一個角色引用許多其他角色(說一個角色可以從多個其他角色繼承) public function children(){ return $this->hasMany(Role::class, "roles_roles", "role_inherit_from_id");

但得到錯誤:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.roles_roles' in 'where clause' (SQL: select * from `roles` where `roles`.`roles_roles` is null and `roles`.`roles_roles` is not null limit 1)
}`

有誰知道如何實現一個可以引用自己的表,或者解決該問題的方法?

也許您需要而不是belongsToMany

   $this->belongsToMany(Role::class, 'roles_roles', 'role_id', 'role_inherit_from_id');

另外,你有錯誤的語法hasMany

$this->hasMany(Model::class, 'foreign_key', 'local_key');

說明文件: belongsToManyhasMany

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM