首页 文章

多对多的雄辩 - 同一型号

提问于
浏览
0

对不起,因为我是Laravel的初学者 .

我有一个名为PACS的模型 . 每个PACS可以与许多其他PACS相关 . 从一个到另一个的关系也有方向,推动或拉动 .

我的PACS模型定义为多对多关系

public function pacsRelation() {
        return $this->belongsToMany('App\PACS', 'pacs_has_pacs', 'pacsId', 'hasPacsId')->withTimestamps()->withPivot('transferRelation');
    }

我的数据透视表是

Schema::create('pacs_has_pacs', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('pacsId')->unsigned();
            $table->integer('hasPacsId')->unsigned();
            $table->enum('transferRelation', ['push', 'pull']);

            $table->foreign('pacsId')->references('pacsId')->on('pacs');
            $table->foreign('hasPacsId')->references('pacsId')->on('pacs');
        });

我的PACS模型表是

Schema::create('pacs', function (Blueprint $table) {
            $table->increments('pacsId');
            $table->timestamps();
            $table->string('email');
            $table->string('name');
            $table->string('fax')->nullable();
            $table->string('edi')->nullable();
            $table->string('contact');
        });

我正在执行以下代码时遇到麻烦,并且我的数据透视表中没有显示任何行并且没有错误 .

public function handle()
    {

        $this->error("The relationship is defined push or pull by how the receiving party is able to retreive images from the sending party.");

        $valid = false;

        while (!$valid) {

            $receiving = $this->ask('Receiving PACS name');

            try {
                $pacs = PACS::where('name', '=', $receiving)->firstOrFail();
            } catch (ModelNotFoundException $e) {
                $this->error('This PACS does not exist');
                continue;
            }

            $valid = true;

        }

        $this->info($pacs);

        $valid = false;

        while (!$valid) {

            $sending = $this->ask('Sending PACS name');

            try {
                $sendingPacs = PACS::where('name', '=', $sending)->firstOrFail();
            } catch (ModelNotFoundException $e) {
                $this->error('This PACS does not exist');
                continue;
            }

            $valid = true;

        }

        $this->info($sendingPacs);

        $relation = $this->choice('Push or Pull relation?', ['push', 'pull']);

        $pacs->pacsRelation()->save($sendingPacs, ['transferRelation'=>$relation]);

        $this->info('Relationship successfully defined.');

    }

有什么明显的东西我缺少或者我错误地走了吗?

1 回答

  • 0

    经过几个小时的观察后,这个问题的解决方案归结为Laravel没有认出我 table 的主键 .

    我不得不定义

    protected $primaryKey = 'pacsId';
    

    在我的PACS模型上,我离开了 .

相关问题