简体   繁体   English

有多个表的belongsToMany(设计建议)

[英]belongsToMany with multiple tables (design suggestion)

I have 5 tables in my laravel/vue project: 我的laravel / vue项目中有5张桌子:

  • Movies 电影
  • Series 系列
  • Anime 日本动漫
  • Actors 演员们
  • Actors relationship 演员关系

Actors' table belongs to Movies, Series, Anime and this many-to-many relationship is registered in the actors_relationship . 演员表属于电影,系列,动漫,并且这种多对多关系已在actors_relationship注册。

I'm able to create a many-to-many relationship in the Actor.php model using the following code: 我可以使用以下代码在Actor.php模型中创建多对多关系:

public function movies() {
    return $this->belongsToMany(MovieModel::class, "actors_relationship", 'actor_id', 'media_id')->wherePivot("media_type", AdminHelper::TYPE_MOVIES['value'])->withTimestamps();
}

Using this code I'm getting the records with the included actor id and has a specific int as a media_type . 使用此代码,我将获得包含actor id的记录,并具有一个特定的int作为media_type

Until this point, I have no problem. 到目前为止,我没有问题。 My problem is getting the count of all of these relationships. 我的问题是获取所有这些关系的数量。 eg getting the count of movies, series, anime and sorting it desc/asc 例如,获取电影,连续剧,动漫的数量并对其进行排序

Solutions: 解决方案:

  1. Retrieving the relationship actors' count from movies, series, anime and register it in a custom attribute . 从电影,系列,动画中检索关系演员的数量,并将其注册到自定义attribute The sorting will be on the client-end. 排序将在客户端上。

  2. Doing a hard code check on the server-side for the sorting, then checking if the sort is by total_count , if yes, then get the collection first and after the ->get() command, try sorting by the custom attribute . 在服务器端执行硬代码检查以进行排序,然后检查是否通过total_count进行排序,如果是,则首先获取集合,然后在->get()命令之后,尝试通过custom attribute排序。

I would like to follow the best practice regarding this issue. 我想遵循有关此问题的最佳实践。 Is there a way to get the count of all relationships and sort by it? 有没有一种方法可以计算所有关系并对其进行排序?

A client-side solution would cause more work because this project will be for Android and Web and therefore will require various modifications in both fields. 客户端解决方案将导致更多工作,因为该项目将用于Android和Web,因此将需要在两个领域中进行各种修改。

Furthermore, I would normally do ->with('movies') and then sort the automatically created field movies_count . 此外,我通常会执行->with('movies') ,然后对自动创建的字段movies_count进行排序。 I would like to have a similar approach. 我想有一个类似的方法。

Tables: 表格:

actors : actors

id actor_title

movies : movies

id movie_title 

series : series

id series_title 

anime : anime

id anime_title

actors_relationship : actors_relationship

id actor_id media_id media_type

EDIT: 编辑:

I'm looking for the best advice to implement a total count of movies , series and anime . 我正在寻找最好的建议,以实施全部的moviesseriesanime This means that I would like the total count of these 3 relations in one field. 这意味着我希望在一个字段中获得这3个关系的总数。 I could use a custom attribute, but then I will need to sort that using PHP/client-side instead of doing it in the SQL query in Eloquent. 我可以使用自定义属性,但随后需要使用PHP /客户端对它进行排序,而不是在Eloquent的SQL查询中进行排序。

This means that I'm looking for the best way that follows the best practices. 这意味着我正在寻找遵循最佳实践的最佳方法。 The total_count will sum up the total of those aforementioned relations. total_count将总结上述关系的总数。 I would like to sort by this new total_count . 我想按这个新的total_count排序。

This seems like a good case for a MorphToMany https://laravel.com/docs/5.8/eloquent-relationships#many-to-many-polymorphic-relations 对于MorphToMany https://laravel.com/docs/5.8/eloquent-relationships#many-to-many-polymorphic-relations来说,这似乎是一个很好的例子。

    class Actor

    public function movies()
        {
            return $this->morphedByMany(MovieModel::class,'media',"actors_relationship");
        }

/// Add a the count to the query 

    public function scopeWithRelationsCount($q)
    {
        /// this is needed to so we keep the select of all the table columns too
        if(!$q->getQuery()->columns){
            $q->select($this->getTable().'.*');
        }

        return $q->selectSub(

                ActorRelationship::selectRaw('count(*)')->whereColumn('actor_id', 'actors.id'),

                'relations_count' /// the alias we are using could be anything  
            )
    }

Then you if you want the total number of series, movies and and anime for each actor ordered by most you can use. 然后,如果您想要大多数人订购的每个演员的剧集,电影和动漫的总数,便可以使用。 Actor::withRelationsCount()->orderBy('relations')->get()

There are some macros that make this look a little nicer. 有一些宏使它看起来更好看。 Take a look at https://github.com/reinink/advanced-eloquent . 看看https://github.com/reinink/advanced-eloquent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM