首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多对多雄辩-相同的模型

多对多雄辩-相同的模型
EN

Stack Overflow用户
提问于 2016-08-31 07:47:04
回答 1查看 195关注 0票数 0

对不起,我是一个相对的Laravel初学者。

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

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

代码语言:javascript
复制
public function pacsRelation() {
        return $this->belongsToMany('App\PACS', 'pacs_has_pacs', 'pacsId', 'hasPacsId')->withTimestamps()->withPivot('transferRelation');
    }

我的数据透视表是

代码语言:javascript
复制
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模型表是

代码语言:javascript
复制
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');
        });

我遇到了麻烦,因为我正在执行以下代码,并且我的数据透视表中没有出现任何行,也没有错误。

代码语言:javascript
复制
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.');

    }

有没有明显的我遗漏了什么,或者我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2016-08-31 09:40:03

经过几个小时的查找后,这个问题的解决方案归结为Laravel无法识别我的表的主键。

我不得不定义

代码语言:javascript
复制
protected $primaryKey = 'pacsId';

在我的PACS机型上,我离开了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39238701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档