简体   繁体   English

Laravel中的多联接表

[英]Multi JOIN Table in Laravel

I have 4 tables below : 我下面有4张桌子:

track 跟踪

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

album 专辑

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

track_artist track_artist

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

artist 艺术家

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

I want to get track.TITLE , album.TITLE and artist.NAME base on track.TID ( where TID = $XXX ) in 4 table above. 我想基于以上4表中的track.TIDwhere TID = $XXX )获取track.TITLEalbum.TITLEartist.NAME It's easy when I use raw INNER JOIN MySQL code, but I want to use Eloquent ORM in Laravel. 当我使用原始的INNER JOIN MySQL代码时很容易,但是我想在Laravel中使用Eloquent ORM。 How I can do this ? 我该怎么做? Thank you. 谢谢。

I have analyzed : 我分析过:

One track belongsTo one Album 首曲目 belongsTo一张专辑

One track hasMany track_artist 一个轨道 hasMany track_artist

One track_artist hasOne artist 一位track_artist有 hasOne 艺术家

One track hasMany artist 一个轨道 hasMany 艺术家

First, your relations ( in my opinion ) need some changes. 首先,你的关系(我认为)需要一些改变。

  • One track belongs to one album and one album has many tracks (one to many) 一首曲目属于一张专辑,而一本专辑具有多首曲目(一对多)
  • One track has one artist and one artist has multiple tracks (one to many) 一首曲目有一位歌手,而一位歌手有多首曲目(一对多)
  • One album has multiple artist and one artist has multiple albums(many to many) 一张专辑有多位歌手,而一位歌手有多张专辑(很多)

You will need a table for the many to many relation with the album id and the artist id 您将需要一个表格,用于与album idartist id多对多关系

This is an example considering the above points 这是考虑以上几点的示例


<?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');
    }
}

You can try this 你可以试试这个

The table should be like this, you need 4 tables 桌子应该是这样,你需要4张桌子

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

Models 楷模

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

Fetch Data 获取数据

$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;
}

Note: You don't need to create model for track_artists because it is a pivot table for tracks and artists tables. 注意:您不需要创建模型track_artists ,因为它是一个透视表tracksartists表。 You can insert and update in pivot table using Track or Artist laravel eloquent model. 您可以使用TrackArtist laravel雄辩模型在数据透视表中插入和更新。 Though if you want you can create it extending Pivot . 不过,如果您愿意,可以创建它扩展Pivot

Save Data in pivot table 将数据保存到数据透视表中

$track = Track::find(1);
$artist = Artist::create(['name' => 'Katy Perry', 'avatar' => 'Katy_Perry.jpg']);
$track->artists()->save($artist); //this save track and artist relation in track_artists pivot table

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

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

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