拉维尔(Laravel)加入不在3个不同的桌子上工作

发布于 2025-02-14 01:46:19 字数 608 浏览 0 评论 0原文

模型

Album::join('tracks', 'albums.track_id', '=', 'tracks.id')
        ->join('singers', 'tracks.singer_id', '=', 'singers.id')->get();

Snigers Table

id - int
name - string

跟踪表

id -int
name - string
singers_id - JSON // example["1","2"]

专辑table

id - int
name - string
tracks_id - JSON // example ["1","2"]


在这里首先Singers Table After table table ID多倍数下一个明矾表参考跟踪表ID也是数组。如何连接这三个表

Model

Album::join('tracks', 'albums.track_id', '=', 'tracks.id')
        ->join('singers', 'tracks.singer_id', '=', 'singers.id')->get();

Snigers table

id - int
name - string

Tracks table

id -int
name - string
singers_id - JSON // example["1","2"]

Album table

id - int
name - string
tracks_id - JSON // example ["1","2"]


here first singers table after track table reference array of id multiple and next alums table reference track table id is also array.how to joins this three table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2025-02-21 01:46:19

无需在此处使用加入,如果您有一个枢轴表,这就是您可以做的:

在模型中,您可以简单地创建这样的关系:

pream模型:

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

public function singers() {
    return $this->hasMany(Singer::class);
}

然后调用它在您的控制器中。

COMPORCONTROLLER.PHP

Album::where('id',1)->with('tracks','singers');

这将简单地使用属于相册ID 1的曲目和歌手获取所有记录。

There's no need to use joins here, if you have a pivot table for these, here's what you can simply do:

In your models you can simply create a relation like this:

Album Model:

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

public function singers() {
    return $this->hasMany(Singer::class);
}

and then call it in your controller.

AlbumController.php

Album::where('id',1)->with('tracks','singers');

This will simply fetch all the records with Tracks and Singers that belong to Album id 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文