Laravel表通过枢轴引用自己

时间:2018-08-18 13:30:00

标签: php laravel

我有一张桌子叫角色:

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)
}`

有人知道如何实现一个可以引用自己的表,或者解决该问题的方法吗?

1 个答案:

答案 0 :(得分:0)

也许您需要let shownQuotes = []; function getRandomQuote () { // if all quotes were used reset shownQuotes so all quotes can be shown again if (shownQuotes.length === quotes.length) { shownQuotes = []; } // filter all quotes that have been used const unusedQuotes = quotes.filter(quote => !shownQuotes.includes(quote)); // get a random index between [0, unusedQuotes.length) const randomIndex = Math.floor(Math.random() * unusedQuotes.length); const randomQuote = unusedQuotes[randomIndex]; // add the element to the quotes already shown. shownQuotes.push(randomQuote); return randomQuote; } 来代替?

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>

此外,您使用了错误的语法belongsToMany

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

文档:belongsToManyhasMany