在 Laravel 5.7 (Eloquent) 中从多个表中获取数据

伊斯兰教徒

我正在使用 Laravel 5.7。我有三个 MySQL 表,名称为:movies, movie_directors, movie_actors我在从这些表中检索数据时遇到问题。看看我的表结构:

movies
|-----------------------------------------------------|
|   id  |   title       |   release |  img_url        |
|-----------------------------------------------------|
|  1    |  Avengers     |    2019   |    #            |
|-----------------------------------------------------|
|  2    |  Avatar       |    2009   |    #            |
|-----------------------------------------------------|
|  3    |  Titanic      |    1997   |    #            |
|-----------------------------------------------------|

directors
|-----------------------------------------------------|
|   id  |   movie_id  |   dir_name      |  add_date   |
|-----------------------------------------------------|
|  1    |    1        |  Anthony Russo  |  2019/02/18 |
|-----------------------------------------------------|
|  2    |    1        |    Joe Russo    |  2019/02/18 |
|-----------------------------------------------------|
|  3    |    2        |    Cameron      |  2019/02/18 |
|-----------------------------------------------------|

actors
|-----------------------------------------------------|
|   id  |   movie_id  |   act_name      |  add_date   |
|-----------------------------------------------------|
|  1    |    1        |  Robert Downey  |  2019/02/18 |
|-----------------------------------------------------|
|  2    |    1        |   Chris Evans   |  2019/02/18 |
|-----------------------------------------------------|
|  3    |    1        |  Mark Ruffalo   |  2019/02/18 |
|-----------------------------------------------------|
|  4    |    1        |   Chris Pratt   |  2019/02/18 |
|-----------------------------------------------------|
|  5    |    2        |   Worthington   |  2019/02/18 |
|-----------------------------------------------------|
|  6    |    2        |    Weaver       |  2019/02/18 |
|-----------------------------------------------------|
|  7    |    2        |    Saldana      |  2019/02/18 |
|-----------------------------------------------------|

I Want
|-------------------------------------------------------------------------------------------------------------------------------------------|
|   id  |   title       |   release |  img_url    |        directors           |                        actors                              |
|-------------------------------------------------------------------------------------------------------------------------------------------|
|  1    |  Avengers     |    2019   |    #        | Anthony Russo, Joe Russo   |  Robert Downey, Chris Evans, Mark Ruffalo, Chris Pratt     | 
|-------------------------------------------------------------------------------------------------------------------------------------------|
|  2    |  Avatar       |    2009   |    #        |     Cameron                |  Worthington, Weaver, Saldana                              |
|-------------------------------------------------------------------------------------------------------------------------------------------|
|  3    |  Titanic      |    1997   |    #        |                            |                                
|-------------------------------------------------------------------------------------------------------------------------------------------|

我正在使用的当前代码:

$movie_list = DB::table('movies')
            ->select('movies.*', 'movie_directors.*', 'movie_actors.*', 'movie_genres.*', 'movie_links.*', 'movie_types.*')
            ->join('movie_directors','movies.id', '=','movie_directors.movie_id')
            ->join('movie_actors', 'movies.id', '=', 'movie_actors.movie_id')
            ->where('movies.status',1)
            ->paginate(50)
            ;

查看代码底部的表格“我想要”。这就是我要的。不要附上下面的链接作为已经回答的证据。如何在laravel eloquent中从多个表中检索数据与我的要求
不符

瓦迪姆·西布

为了实现这一点,您需要按movies.id 对结果进行分组,并使用group_concat逗号连接来提取结果。您的代码应如下所示:

$movie_list = DB::table('movies')
    ->select(
        'movies.*', 
        DB::raw('group_concat(movie_directors.dir_name) as directors'),
        DB::raw('group_concat(movie_actors.act_name) as actors')
    )
    ->join('movie_directors','movies.id', '=','movie_directors.movie_id')
    ->join('movie_actors', 'movies.id', '=', 'movie_actors.movie_id')
    ->where('movies.status',1)
    ->groupBy('movies.id')
    ->paginate(50)
    ; 

您可以group_concat在此处阅读更多信息https : //www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Laravel Eloquent:从两个表中检索数据

Laravel Eloquent:对两个表中的数据进行计数

Laravel Eloquent:在Eloquent中获取联接表的ID字段

将Laravel 5 Eloquent模型移到其自己的目录中

如何使用多个外键查询数据透视表Laravel 5 Eloquent

Laravel Eloquent从多个表中检索数据

如何在Laravel 5中使用Eloquent更新数据透视表

与Eloquent Laravel 5的交易

从多个表中获取数据Laravel

在Laravel 5中使用ORM Eloquent访问DBF表

从Laravel中的多个表获取数据

Laravel Eloquent:从2个表中获取JSON数据

如何使用Laravel Eloquent从多个表中获取数据

Laravel 5 Eloquent与MongoDB-从文档中获取列名数组

Laravel 5模型:在Eloquent中不要包含某些列

在Laravel和Eloquent中获取2个以上联接表的数据

Laravel 5 Eloquent 在关系中搜索不起作用

如何在 Laravel 5 中编写动态数量的 Eloquent 查询?

Laravel 5 - 覆盖从 Eloquent 获取或选择

Laravel Eloquent 获取与数据透视表中的值匹配的关系

Foreach 插入 Laravel 5 Eloquent

Laravel Dingo 从多个表中获取数据

Laravel Eloquent - 数据透视表

如何在laravel eloquent中检索具有多个条件的多个表数据?

Laravel 5 Eloquent AND 而不是 where

如何使用laravel eloquent从其他表中获取和显示数据

如何使用集合地图对laravel 7 eloquent中的数据进行排序?

Laravel Eloquent 从关系中获取数据

如何在 Laravel eloquent 中查询数据透视表的计数