【问题标题】:How can I form a query that results includes only the common values from two separate tables in laravel? [closed]如何形成一个查询,其结果仅包含来自 laravel 中两个单独表的公共值? [关闭]
【发布时间】:2019-12-21 11:04:56
【问题描述】:

我们正在尝试将报告中的结果返回到结构,其中仅显示具有共同值(来自两个单独的数据表)的报告用户。

我们正在使用幼虫。在控制器中,我们将“报告用户”定义为活跃的用户(来自公共用户表),向另一个“user_id”报告,他们都共享来自($dbPrefix.'access_permissions' 和 $ 的完全相同的“scorecard_type_id”) dbPrefix.'scored_in')。我们尝试过使用内部连接,但出现错误。这适用于在 larvel 堆栈下运行 MySQL 和 PHP 的 linux 服务器。

Scored_in 表的图像(使用的列 ID:“userid”和“score_card_type_id”):

Access_Permissions 表的图像(使用的列 ID:“id”和“score_card_type_id”):

对于用户表:我们引用的 Id 是“id”

在这个场景中使用了三个数据表,一个普通用户表,一个用于 score_in 的前缀数据库表(该表定义了 score_card_type_id 用户被评分)和一个用于 access_permissions 的前缀数据库表(这是表定义了一个用户可以给其他用户打分的 score_card_type_id)

我们目前所处的位置: 您可以在这里看到教师 1 可以看到学生 2 和学生 3,但他们应该看不到教师 2 或教师 3 班级中的学生 2 和学生 3,因为教师 1 不教学生 2 或学生 3。 我们想要什么:

我们希望用户(教师 1)只看到向教师 1 报告的学生,并且在教师 1 在 access_permissions score_card_type_id(s) 中的 score_card_type_id(s) 中得分

function getAllReportingUsers($userId, &$allUsers) {
    $dbPrefix = $this->getDBPrefix(null);
    $user_profiles =  user_profile::join('users', 'users.id', '=', 'user_profile.user_id')
    ->where('users.is_active', '=',  1)

    ->where('user_profile.reports_to', '=',  $userId)

    // Shit!
    //->select(DB::raw('distinct('.$dbPrefix.'scored_in.score_card_type_id)'))
    //->where($dbPrefix.'scored_in.score_card_type_id', '=', $dbPrefix.'access_permission.score_card_type_id')


    ->get();

    foreach ($user_profiles as  $reportUserId){

        $userReportTo_id = $reportUserId->user_id;

        if($userReportTo_id == $userId)

        {

            array_push($allUsers, $userReportTo_id);

            return false;

        }

        if($userReportTo_id>0){

            array_push($allUsers, $userReportTo_id);

            $this->getAllReportingUsers($userReportTo_id, $allUsers);

        }else{

            return false;

        }

我们希望结果是查看用户只能看到“scored_in”数据表中“scorecard_type_id”=access_permissions 数据表中查看用户“scorecard_type_id”的报告用户。

【问题讨论】:

标签: php mysql laravel inner-join


【解决方案1】:

这很难理解,并且此代码存在一些问题。

首先,您应该使用 Eloquent 关系 (https://laravel.com/docs/5.8/eloquent-relationships) 而不是联接。

其次,除非有像a reports to b, c reports to b, therefore c reports to a 这样的报告链,否则您不需要this->getAllReportingUsers().. 行,因为它会产生不必要的递归。我也不确定你为什么通过引用传递而不返回输出,但我会把它留给你。

第三,试试这个作为查询(你可以在不修复上述问题的情况下这样做):

$user_profiles =  user_profile::join('users','users.id','=','user_profile.user_id')
                      ->join('scored_in','scored_in.userId','=','users.id')
                      ->join('access_permissions','access_permissions.score_card_type_id','=','scored_in.score_card_type_id')
                      ->where('users.is_active', '=',  1)
                      ->where('user_profile.reports_to', '=',  $userId)
                      ->where('access_permissions.id', '=', $userId)
                      ->get()

这是做什么的:

  1. 获取所有 user_profiles 并将它们加入用户
  2. 将记分卡加入用户
  3. 加入用户访问记分卡
  4. 过滤掉不活跃的用户
  5. 过滤掉不向$userId报告的用户
  6. 过滤掉$userId 无权访问记分卡的用户

请注意,如果存在共享相同名称的列,您可能会在此处遇到一些有趣的行为,因为后面的列名称会覆盖连接中较早的列名称。

希望我对你的理解正确......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多