【问题标题】:Get data from multiple table in Laravel 5.7 (Eloquent)从 Laravel 5.7 (Eloquent) 中的多个表中获取数据
【发布时间】:2020-02-19 15:05:49
【问题描述】:

我正在使用 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)
            ;

查看代码底部的“我想要”表格。这就是我要的。 不要附上下面的链接作为已经回答的证据。不符合我的要求
how to retrive data from multiple table in laravel eloquent

【问题讨论】:

  • 您是如何尝试检索数据的?你有任何代码吗?
  • @VadimSirbu ,刚刚添加了我正在使用的代码。

标签: php laravel-5 eloquent


【解决方案1】:

为了实现这一点,您需要按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

【讨论】:

  • 返回此错误消息:Base table or view not found: 1051 Unknown table 'mvz.movie_genres' ..... 有什么想法吗?
  • 我刚刚调整了你提供的代码,我不知道你有什么表。我假设您应该从您的选择中删除以下'movie_genres.*''movie_links.*''movie_types.*'
  • 在你的描述中你提到的表名是directorsactors,但是在你的查询中你使用movie_directorsmovie_actors
  • movie_directorsmovie_actors 这些是实际的表名。正如您提到的,我还从查询中删除了其他表。现在我得到这个错误:Syntax error or access violation: 1055 'mvz.movies.title' isn't in GROUP BY...
  • 你能不能试试这个,看看有没有结果:$movie_list = DB::table('movies') ->select( 'movies.id', 'movies.title', 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', 'movies.title') ->paginate(50) ;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2021-01-19
  • 2016-09-09
  • 2016-06-24
  • 2013-05-17
相关资源
最近更新 更多