Laravel 5 SQL连接3个表但返回null

时间:2017-04-08 14:01:25

标签: php sql laravel

我有3张桌子。

  1. 名称:目录 enter image description here

  2. 名称: Directory_relations enter image description here

  3. 姓名:视频

    enter image description here

  4. 我的问题是当我想要加入3个表时。因为当我使用Directory_relations加入Directory时,它显示正确但只有文件夹,但是当我加入vidoes时,什么都不显示。有可能显示所有这些。

    在下面的代码中,错过ROW在文件夹3中使用VIDEO_ID。

    array:1 [▼
          0 => array:9 [▼
            "id" => "4"
            "video_id" => "0"
            "directory_id" => "4"
            "parent_id" => "0"
            "created_at" => "-0001-11-30 00:00:00"
            "updated_at" => "-0001-11-30 00:00:00"
            "name" => "Folder2"
            "user_id" => "265"
            "children" => array:2 [▼
              0 => array:9 [▼
                "id" => "5"
                "video_id" => "0"
                "directory_id" => "5"
                "parent_id" => "4"
                "created_at" => "-0001-11-30 00:00:00"
                "updated_at" => "-0001-11-30 00:00:00"
                "name" => "Folder3"
                "user_id" => "265"
                "children" => array:1 [▼
                  0 => array:8 [▼
                    "id" => "7"
                    "video_id" => "0"
                    "directory_id" => "7"
                    "parent_id" => "5"
                    "created_at" => "-0001-11-30 00:00:00"
                    "updated_at" => "-0001-11-30 00:00:00"
                    "name" => "Folder33"
                    "user_id" => "265"
                  ]
                ]
              ]
              1 => array:8 [▼
                "id" => "6"
                "video_id" => "0"
                "directory_id" => "6"
                "parent_id" => "4"
                "created_at" => "-0001-11-30 00:00:00"
                "updated_at" => "-0001-11-30 00:00:00"
                "name" => "Folder22"
                "user_id" => "265"
              ]
            ]
          ]
        ]
    

    有2个连接(带文件夹)仅显示没有视频的文件夹:

    $dir = Directory::select('directory_relations.*','directory.*')->join('directory_relations', 'directory_relations.directory_id', '=', 'directory.id')->where('user_id',Auth::user()->id)->get();
    

    有3个连接(文件夹+视频)没有显示任何内容:     $ dir = Directory::select('directory_relations.*','directory.*','videos.*')->join('directory_relations', 'directory_relations.directory_id', '=', 'directory.id')->join('videos', 'directory_relations.video_id', '=', 'videos.id')->where('directory.user_id',Auth::user()->id)->get();

0 个答案:

没有答案