2016-08-30 26 views
0

私は相対的なラーベルの初心者です。多対多人数同型

私は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.'); 

    } 

明らかに私が行方不明か間違った方法で行ったことがありますか?

+0

'attach()'を見て... https://laravel.com/docs/5.3/eloquent-relationships#updating-many-to-many-relationships – Sherif

答えて

0

多くの時間をかけてこの問題を解決すると、Laravelは私のテーブルの主キーを認識しませんでした。

私は私のPACSモデルに

protected $primaryKey = 'pacsId'; 

を定義しなければならなかったと私は離れていました。