Wiele do wielu relacji z taksonomią w Eloquent
Używam Laravel 4. Mam wiele do wielu relacji w moim systemie. I wybieram schemat tabeli taksonomii Wordpress.
Ale jak mogę tworzyć relacje modelowe z Laravel 4 Eloquent ORM? Oto moje tabele baz danych;
Stółterms
:+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | MUL | | |
| slug | varchar(200) | NO | UNI | | |
+------------+---------------------+------+-----+---------+----------------+
Stółterm_taxonomy
:+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| term_id | bigint(20) unsigned | NO | MUL | 0 | |
| taxonomy | varchar(32) | NO | MUL | | |
| description | longtext | NO | | NULL | |
| parent | bigint(20) unsigned | NO | | 0 | |
| count | bigint(20) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+----------------+
Stółterm_relationships
:+------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+-------+
| object_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_order | int(11) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+-------+
Normalnie możemy to zrobićreturn $this->belongsToMany('Term');
ale jak możemy zrobić 2 relacje? Potrzebujemy 2 relacji najpierw znaleźć terminologię z tabeli „term_taxonomy”, po znalezieniu relacji relacje z „taxonomy_id”.
I przykład tego, jak chcę używać;
$categories = Post::find(1)->categories; // get terms with taxonomy="post_category"
$tags = Post::find(1)->tags; // get terms with taxonomy="post_tag"
Nie chcę tego robić z podstawową klasą bazy danych ”DB::table('table')->join('...')...
„Chcę używać metod i modeli relacji Eloquent.