2016-08-30 3 views
0

Sorry wie ich ein relativer Laravel Anfänger bin.Viele zu viele eloquent - Same Model

Ich habe ein Modell namens PACS. Jedes PACS kann mit vielen anderen PACS verknüpft sein. Die Beziehung von einem zum anderen hat auch eine Richtung, Push oder Pull.

Mein PACS Modell hat eine viele zu viele Beziehung definiert als

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

meine Pivot-Tabelle ist

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'); 
     }); 

mein PACS Modelltisch ist

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'); 
     }); 

ich Probleme habe als Ich führe den folgenden Code aus und habe keine Zeilen in meiner Pivot-Tabelle und keine Fehler angezeigt.

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

    } 

Gibt es etwas Offensichtliches, das ich vermisse oder bin ich falsch herumgegangen?

+0

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

Antwort

0

Die Lösung für dieses Problem nach vielen Stunden der Suche kam zu Laravel nicht den Primärschlüssel meiner Tabelle zu erkennen.

Ich hatte

protected $primaryKey = 'pacsId'; 

auf meinem PACS-Modell zu definieren, und ich war weg.