From many to many relationships with taxonomy in Oratory

I use Laravel 4. I have many different relationships on my system. And I prefer to use the Wordpress taxonomy table schema.

But how can I establish model relationships with the Laravel 4 Eloquent ORM? Here are my database tables;

Table 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 |         |                |
+------------+---------------------+------+-----+---------+----------------+

Table 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       |                |
+------------------+---------------------+------+-----+---------+----------------+

Table 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       |       |
+------------------+---------------------+------+-----+---------+-------+

Usually we can do return $this->belongsToMany('Term');, but how can we do 2 relationships? We need 2 relationships that first find the taxonomy terminology from the term_taxonomy table after finding the relationship with taxonomy_id.

And an example of how I want to use;

$categories = Post::find(1)->categories; // get terms with taxonomy="post_category" 
$tags = Post::find(1)->tags; // get terms with taxonomy="post_tag" 

I don’t want to do this with the database base class " DB::table('table')->join('...')..." I want to use the Brightness relationship methods and models.

+5
1

getter :

Post , :

public function getCategories()
{
    return $this->hasMany()->where('taxonomy', 'post_category');
}

public function getTags()
{
    return $this->hasMany()->where('taxonomy', 'post_tag');
}
+2

All Articles