Laravel中的多联接表

时间:2018-06-26 15:14:05

标签: php mysql laravel eloquent

我在下面有4张桌子:

曲目

+----+-----+----------------+-------+
| ID | TID |     TITLE      | ALBUM |
+----+-----+----------------+-------+
|  1 | AAA | Yesterday      |     1 |
|  2 | BBB | Happy          |     2 |
|  3 | CCC | Gangname Style |     3 |
+----+-----+----------------+-------+

专辑

+----+-----+---------+-------+
| ID | AID |  TITLE  | COVER |
+----+-----+---------+-------+
|  1 | AAA | Album A | 1.jpg |
|  2 | BBB | Album B | 2.jpg |
|  3 | CCC | Album C | 3.jpg |
+----+-----+---------+-------+

track_artist

+----+-----+-----------+
| ID | TID | ARTIST_ID |
+----+-----+-----------+
|  1 |   1 |         1 |
|  2 |   2 |         2 |
|  3 |   3 |         3 |
+----+-----+-----------+

艺术家

+----+--------+--------+
| ID |  NAME  | AVATAR |
+----+--------+--------+
|  1 | Taylor | 1.JPG  |
|  2 | T-ara  | 2.JPG  |
|  3 | M2M    | 3.JPG  |
+----+--------+--------+

我想基于上面4表中的track.TITLEalbum.TITLE)获得artist.NAMEtrack.TIDwhere TID = $XXX。当我使用原始的INNER JOIN MySQL代码时很容易,但是我想在Laravel中使用Eloquent ORM。我该怎么做?谢谢。

我已经分析了:

一条曲目 belongsTo一条专辑

一条曲目 hasMany track_artist

一位 track_artist hasOne 艺术家

一条曲目 hasMany 艺术家

2 个答案:

答案 0 :(得分:0)

首先,您的关系(我认为)需要一些更改。

  • 一首曲目属于一张专辑,而一本专辑具有许多曲目(一对多)
  • 一首曲目有一位歌手,而一位歌手有多首曲目(一对多)
  • 一张专辑有多位歌手,而一位歌手有多张专辑(多对多)

您将需要一个与album idartist id的多对多关系的表

这是考虑以上几点的示例


<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class track extends Model{
    function album(){
        $this->belongsTo(album::class);
    }
    function artist(){
        $this->belongsTo(artist::class);
    }
}

class album extends Model{
    function track(){
        $this->hasMany(track::class);
    }
    function artist(){
        $this->belongsToMany(artist::class, 'my_many_to_many_table');
    }
}

class artist extends Model{
    function track(){
        $this->hasMany(track::class);
    }
    function album(){
        $this->belongsToMany(album::class, 'my_many_to_many_table');
    }
}

答案 1 :(得分:0)

您可以尝试

  

桌子应该是这样,您需要4张桌子

public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (null != row) yield return row;
        }
    }

    private void DgCustomerInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        try
        {
            var row_list = GetDataGridRows(customersPage.dgCustomerInfo);
            foreach (DataGridRow single_row in row_list)
            {
                if (single_row.IsSelected == true)
                {

                    customersPage.txtCustomerName.Text=single_row.Item.ToString();

                }
            }

        }
  

模型

albums(id, title, cover)
artist(id, name, avatar)
tracks(id, album_id, title)
track_artists (id, artist_id, track_id) ManyToMany with tracks
  

获取数据

class Album extends Model{

    function tracks(){
        $this->hasMany(Track::class);
    }
}

class Artist extends Model{

   function tracks(){
      $this->belongsToMany(Track::class, 'track_artists'); //here track_artists table is as pivot table
   }   
}

class Track extends Model{

   function album(){
        $this->belongsTo(Album::class);
   }

   function artist(){
      $this->belongsToMany(Artist::class, 'track_artists'); //here track_artists table is as pivot table
   }   
}

注意:您无需为$track = Track::with('album','artists')->find($trackId); echo $track->title." - ". $track->album->title; //now print all artist of this track foreach($track->artists as $artist){ echo $artist->name; } 创建模型,因为它是track_artiststracks表的数据透视表。您可以使用artistsTrack laravel雄辩的模型在数据透视表中插入和更新。不过,如果您愿意,可以创建扩展Artist的文件。

  

将数据保存在数据透视表中

Pivot

有关详细信息,https://laravel.com/docs/5.6/eloquent-relationships#many-to-many