Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Laravel列出了受欢迎的子模型音乐类型_Mysql_Laravel_Model - Fatal编程技术网

Mysql Laravel列出了受欢迎的子模型音乐类型

Mysql Laravel列出了受欢迎的子模型音乐类型,mysql,laravel,model,Mysql,Laravel,Model,我有4张桌子: MusicGenre Artist 歌曲 SongInfo weekly\u hit是SongInfo表中的一列 MusicGenre连接到Artist,如下所示: public function artists() { return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id'); } public function songs() { return $this->has

我有4张桌子:

  • MusicGenre
  • Artist
  • 歌曲
  • SongInfo
  • weekly\u hit
    SongInfo
    表中的一列

    MusicGenre
    连接到
    Artist
    ,如下所示:

    public function artists()
    {
        return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
    }
    
    public function songs()
    {
        return $this->hasMany('App\Models\Song\Songs','artist_id');
    }
    
    public function info()
    {
        return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
    }
    
    Artist
    连接到
    Song
    ,如下所示:

    public function artists()
    {
        return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
    }
    
    public function songs()
    {
        return $this->hasMany('App\Models\Song\Songs','artist_id');
    }
    
    public function info()
    {
        return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
    }
    
    Song
    连接到
    SongInfo
    ,如下所示:

    public function artists()
    {
        return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
    }
    
    public function songs()
    {
        return $this->hasMany('App\Models\Song\Songs','artist_id');
    }
    
    public function info()
    {
        return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
    }
    
    查询该表没有问题

    我的问题是,我想使用
    SongInfo
    表格中的
    weekly\u hit
    获得最佳音乐类型

    编辑:

    我用原始代码解决了这个问题

    "select music_genres.*, 
    sum(distinct song_info.weekly_hit) as song_popularity
    
    from `music_genres` 
    
    left join `artist_info` on
    `music_genres`.`id` = `artist_info`.`genre_id`
    
    left join songs on 
    artist_info.artist_id = songs.artist_id
    
    left join songs_info on 
    songs.id = songs_info.song_id
    
    group by music_genres.name
    
    order by song_popularity DESC
    
    limit 5
    
    但是,我找不到歌曲。我想从所有返回的音乐类型中获得5首歌曲,按歌曲信息表中的每周热门歌曲排序

    伙计们,我还在寻找解决方案吗


    有人能帮我吗?

    你需要使用Eloquent的hasManyThrough()关系来深入到关系链的深处。

    听起来你需要遍历
    歌曲
    ,获得每首歌曲的
    歌曲id
    ,然后查询每个
    SongInfo
    ,在临时位置查找并记录
    weekly\u hit
    ,以及
    song\u id
    。然后,您可以按每周点击次数对歌曲id进行排序。