Home > Article > Backend Development > About laravel cross-table query problem
I have three tables
article table
Relationship table
Classification table
The object_id in the relationship table corresponds to the id of the article table
term_taxonomy_id corresponds to the term_id of the classification table
Please tell me how I can use eloquent to get the article table and category table through many-to-many method and return them together?
I have three tables
article table
Relationship table
Classification table
The object_id in the relationship table corresponds to the id of the article table
term_taxonomy_id corresponds to the term_id of the classification table
Please tell me how I can use eloquent to get the article table and category table through many-to-many method and return them together?
<code class="php">class Article extends Model{ public function category() { return $this->belongsToMany('App\Categorys', 'relations', 'term_taxonomy_id', 'object_id'); } } $res = Article::with('category')->where('id', $id)->get(); </code>
Reference: https://segmentfault.com/q/10..., use with
RTFMhttps://laravel-china.org/doc...