问题描述
每篇文章属于很多类别。每个用户都有很多类别。我想检索所有有用户类别的文章。对于下面的每个关系,我有自己的数据透视表。
article_cat
id | articleID | categoryId
users_cats
id | user_details_id | categories_id
关系。
Articles.php
public函数类别()
{
return $ this-> belongsToMany('App \Categories','article_categories','articleID','categoryID');
}
UserDetails.php
public function Categories(){
return $ this-> belongsToMany('App \Categories','users_cats','user_details_id' ,'categories_id');
}
Categories.php
public function articles(){
return $ this-> belongsToMany('App \Article','article_categories','categoryID' ,'articleID');
}
public function UserDetails(){
return $ this-> HasMany('App \UserDetails','users_cats','user_details_id','categories_id');
}
我尝试使用HasMany,但它不适用于许多尽可能多的关系。
目前(作为一种解决方案)我一直在使用这个。 Ive提取了用户类别的列表,并搜索所有的ids并拉扯相关的文章,并在UserDetails.php中形成一个集合。
$ user = self :: find($ this-> id);
$ user = $ user-> Categories;
foreach($ user as $ item){
foreach($ item-> article as $ article)
$ article1 [] = Article :: find($ article- > ID);
}
$ articles = collect($ article1) - > sortByDesc('date') - > unique();
返回$文章;
然而,我不认为它会随着数据的增加而扩大(它已经产生超过1k的查询只有1000篇文章,超过8秒加载)。
任何想法?
您可以使用IN查询。或者一个子查询。
文章:: whereIn('id',$ user-> Categories :: all-> ) - > toArray())
$ category_id = $ user-> Categories :: all-> lists('id') - > toArray );
文章:: whereIn('id',function($ query)use($ category_id){
$ query-> select('categories_id')
- > from(with new category) - > getTable())
- > whereIn('category_id',$ categoryID)
}) - > get();
Each Article belongs to many Categories. Each User has many Categories.I want to retrieve all the Articles which has the User’s Categories. For each of these relationships below I have my own pivot tables.
article_cat
id | articleID | categoryId
users_cats
id | user_details_id | categories_id
Relationships.
Articles.php
public function categories()
{
return $this->belongsToMany('App\Categories', 'article_categories', 'articleID', 'categoryID');
}
UserDetails.php
public function Categories(){
return $this->belongsToMany('App\Categories', 'users_cats', 'user_details_id', 'categories_id');
}
Categories.php
public function articles(){
return $this->belongsToMany('App\Article', 'article_categories', 'categoryID', 'articleID');
}
public function UserDetails(){
return $this->HasMany('App\UserDetails', 'users_cats', 'user_details_id', 'categories_id');
}
I have tried to use HasMany through but It doesn’t work with a many to many relationship as far as I can tell.
Currently (as a sort of "work around") Ive been using this. Ive pulled up a list of the user's Categories and searching through all of ids and pulling the relevant Article and forming a collection from it in UserDetails.php
$user = self::find($this->id);
$user = $user->Categories;
foreach ($user as $item) {
foreach ($item->articles as $article)
$article1[] = Article::find($article->id);
}
$articles = collect($article1)->sortByDesc('date')->unique();
return $articles;
However I don’t think it will scale well with increasing data (its already producing over 1k queries with only 1000 articles, taking over 8 seconds to load).
Any ideas?
You could use an IN query. Or a subquery.
Article::whereIn('id', $user->Categories::all->lists('id')->toArray() )
$category_id = $user->Categories::all->lists('id')->toArray();
Article::whereIn('id', function($query) use ($category_id){
$query->select('categories_id')
->from(with(new Category)->getTable())
->whereIn('category_id', $categoryID )
})->get();
这篇关于关系有许多与许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!