Muchas a muchas relaciones con la taxonomía en Eloquent
Estoy usando Laravel 4. Tengo muchas a muchas relaciones en mi sistema. Y elijo usar el esquema de tabla de taxonomía de Wordpress.
Pero, ¿cómo puedo establecer relaciones de modelos con Laravel 4 Eloquent ORM? Aquí están mis tablas de base de datos;
Mesaterms
:+------------+---------------------+------+-----+---------+----------------+
| 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 | | |
+------------+---------------------+------+-----+---------+----------------+
Mesaterm_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 | |
+------------------+---------------------+------+-----+---------+----------------+
Mesaterm_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 | |
+------------------+---------------------+------+-----+---------+-------+
Normalmente podemos hacerreturn $this->belongsToMany('Term');
Pero, ¿cómo podemos hacer 2 relaciones? Necesitamos 2 relaciones primero encontrar taxonomía de término de la tabla "term_taxonomy", después de encontrar relaciones de término con "taxonomy_id".
Y un ejemplo de cómo quiero usar;
$categories = Post::find(1)->categories; // get terms with taxonomy="post_category"
$tags = Post::find(1)->tags; // get terms with taxonomy="post_tag"
No quiero hacer esto con la clase de base de datos básica "DB::table('table')->join('...')...
"Quiero usar métodos y modelos de relaciones elocuentes.