Many to many relationships with taxonomy in Eloquent

给你一囗甜甜゛ 提交于 2019-12-06 03:25:03

问题


I'm using Laravel 4. I have many to many relationships in my system. And I choose to use Wordpress taxonomy table scheme.

But how can I make models relationships with Laravel 4 Eloquent ORM? Here is 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       |       |
+------------------+---------------------+------+-----+---------+-------+

Normally we can do return $this->belongsToMany('Term'); but how can we do 2 relationships? We need 2 relationships first find term taxonomy from "term_taxonomy" table, after find term relations with "taxonomy_id".

And an example for 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 basic database class "DB::table('table')->join('...')..." I want to use Eloquent relation methods and models.


回答1:


You can create getter methods to handle these:

In your Post model, create new methods something along the lines of:

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

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


来源:https://stackoverflow.com/questions/16064976/many-to-many-relationships-with-taxonomy-in-eloquent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!