2016-02-28 15 views
5

Czytam dokuczliwe dokumenty w wersji 5.0, aby zaimplementować wiele do wielu relacji polimorficznych w mojej aplikacji Laravel. Mam wiele modeli takich jak Blog, Question, Photo itd. I chcę mieć system znakowania dla nich wszystkich. Stworzyłem tabelę Tag z następujących schematówJak korzystać z wielu do wielu relacji polimorficznych w Laravel 5.2

Schema::create('tags', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('slug')->unique(); 
     $table->timestamps(); 
    }); 

Poniżej jest schemat pivot table. Nazwa tabeli przestawnej jest entity_tags

Schema::create('entity_tags', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->integer('tag_id')->unsigned();; 
    $table->integer('taggable_id')->unsigned(); 
    $table->string('taggable_type'); 
    $table->timestamps(); 
    $table->index('tag_id'); 
    $table->index('taggable_id'); 
    $table->index('taggable_type'); 
}); 

Jest to związek określony w Tag modelu dla Question modelu

public function questions() 
{ 
    return $this->belongsToMany('App\Question', 'entity_tags', 'tag_id', 'taggable_id'); 
} 

I następująca zależność jest określona w Question Modelu

public function tags() 
{ 
    return $this->belongsToMany('App\Tag', 'entity_tags', 'taggable_id', 'tag_id'); 
} 

Teraz chcę Zdefiniuj wiele do wielu relacji polimorficznych zdefiniowanych w Laravel 5.2.
Moje pytanie to

  • w jaki sposób mogę je zdefiniować?
  • Czy powinienem usunąć relację Wiele do wielu i zdefiniować tylko wiele zależności polimorficznych? Jeśli tak, to w jaki sposób zarządzać niestandardową nazwą tabeli przestawnej?
  • Czy wymagane jest również dodanie nazwy kolumny ze słowem able, które są częścią polimorficznego związku ?

Odpowiedz

4
  • Zastosowanie return $ this-> morphToMany() zamiast belongsToMany, aw modelu tagów, zapis 3 metody z return $ this-> morphedByMany() na odwrotnej relacji.

  • Potrzebne są tylko definicje polimorficzne, bez potrzeby wielu dla wielu normalnych. Nazwa tabeli przestawnej jest na końcu "w stanie" zgodna z domyślną konwencją, ale możesz nadać jej dowolną nazwę.

  • nie, nie musisz mieć słowa z "zdolnym" na końcu, to tylko sposób na określenie, że jest to coś bardziej ogólnego, możesz nazwać to, co chcesz.

Nazewnictwo opiera się na pewnej domyślnej konwencji Laravel.

Aktualizacja:

masz następujący schemat tabeli przestawnej:

Schema::create('entity_tags', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('tag_id')->unsigned();; 
     $table->integer('entity_id')->unsigned(); 
     $table->string('entity_type'); 
     $table->timestamps(); 
     $table->index('tag_id'); 
     $table->index('entity_id'); 
     $table->index('entity_type'); 
}); 

i tabela tags:

Schema::create('tags', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('name'); 
    $table->string('slug')->unique(); 
    $table->timestamps(); 
}); 

więc chcesz tworzyć relacje na blogu, filmu i pytania tabele/modele:

Tag.php Model:

public function questions() 
{ 
    return $this->morphedByMany('App\Question', 'entity', 'entity_tags'); 
} 

public function blogs() 
{ 
    return $this->morphedByMany('App\Blog', 'entity', 'entity_tags'); 
} 

public function videos() 
{ 
    return $this->morphedByMany('App\Video', 'entity', 'entity_tags'); 
} 

Question.php/Blog.php/Video.php

public function tags() 
{ 
    return $this->morphToMany('App\Tag', 'entity', 'entity_tags'); 
} 
+0

Próbowałem rozwiązanie, ale poniżej jest błąd, kiedy oszczędność nowe pytanie 'SQLSTATE [42S22] : Nie znaleziono kolumny: 1054 Nieznana kolumna "tagz_type" w "klauzuli where" (SQL: wybierz "entity_type" z "entity_tags" gdzie "entity_id" = 4 i "tagz_type" = App \ Question) ' Myślę, że drugi parametr powinno być 'xyz'. –

+0

Jeszcze jedno, zamiast' xyz_id' i 'xyz_type', użyłem' entity_id' i 'entity_type' –

Powiązane problemy