Home  >  Article  >  Backend Development  >  About laravel cross-table query problem

About laravel cross-table query problem

WBOY
WBOYOriginal
2016-12-05 13:44:141230browse

I have three tables
article table
About laravel cross-table query problem

Relationship table

About laravel cross-table query problem

Classification table

About laravel cross-table query problem

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?

Reply content:

I have three tables
article table
About laravel cross-table query problem

Relationship table

About laravel cross-table query problem

Classification table

About laravel cross-table query problem

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...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn