laravel nova - 如何制作一个有限制的自引用模型

问题描述

我有一个基于下表的 Laravel 模型:

public function up()
{
    Schema::create('things',function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->string('label');
        $table->foreignId('user_id')->nullable()->constrained('users');
    });

还有一个数据透视表,使它成为多对多的自引用模型。

  public function up()
  {
    Schema::create('thing_thing',function (Blueprint $table) {
      $table->id();
      $table->timestamps();
      $table->string('message')->nullable();
      $table->unsignedBigInteger('parent_id')->nullable();
      $table->unsignedBigInteger('child_id')->nullable();
      $table->unique(['parent_id','child_id']);
      $table->foreign('parent_id')->references('id')->on('things')->onDelete('cascade');
      $table->foreign('child_id')->references('id')->on('things')->onDelete('cascade');
    });
  }

当我创建链接到此模型的 Nova 资源时,我想限制将 thing 附加到自身。因此,例如,带有 thingid = 1 不会出现在带有 id = 1 的事物的附件的选择器中。这是我的 Nova 资源:

  public function fields(Request $request)
  {
    return [
      ID::make(__('ID'),'id')->sortable(),Text::make('label'),ID::make('user_id')->hideWhenUpdating()->hideWhenCreating(),BelongsToMany::make('Trees','trees'),BelongsToMany::make('Things','childOf'),'parentOf')
    ];
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)