微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-使用多个Laravel范围进行关系过滤时出现意外结果

我正在使用Laravel 5.6,并且正在尝试过滤User模型中的关系.用户可以参加课程,这些课程具有要点.

用户可以通过参加课程获得这些积分.这是BelongsToMany关系.

我尝试在此用户模型中创建一个范围,该范围仅包括几年内参加的课程.

/**
 * Retrieves the courses which the user has attended
 */
public function attendedCourses()
{
    return $this->belongsToMany(Course::class, 'course_attendees');
}

/**
 * Searches the user model
 *
 * @param \Illuminate\Database\Eloquent\Builder $builder
 * @param array                                 $years
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeAttendedCoursesInYears(Builder $builder, array $years)
{
    # Filter on the years
    $callback = function($q) use ($years) {
        foreach ($years as $year) {
            $q->year($year);
        }
    };

    return $builder->with(['attendedCourses' => $callback]);
}

在我的课程模型中,我有一个范围可以过滤该课程所在的年份.

public function scopeYear(Builder $query, int $year)
{
    return $query->whereYear('end_date', $year);
}

希望有了这个有人参与的课程年度范围,我希望我可以使用课程模型上的其他范围,通过汇总课程点数来为每个用户计算点数.

public function scopeExternal(Builder $query, bool $flag = true)
{
    $categoryIsExternal = function($query) use ($flag) {
        $query->external($flag);
    };

    return $query->whereHas('category', $categoryIsExternal);
}

在我的CourseCategory模式中,范围如下所示:

/**
 * Scope a query to only include external categories.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 *
 * @param bool                                  $flag
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeExternal(Builder $query, $flag = true)
{
    return $query->where('type', '=', $flag ? 'Extern' : 'Intern');
}

为了计算这个,我尝试做这样的事情.

# Retrieve all the active users
$users = User::all()->attendedCoursesInYears($years);
$test = $users->find(123);

# Calculate the points
$test->attendedCourses()->external(false)->sum('points');

但是,这返回了所有课程的总和.

如我所见,使用范围是这里的唯一选择.我想使用这样的访问器从这些值创建自定义属性.这样,我可以轻松地对计算得出的值进行排序.

/**
 * The users internal course points
 *
 * @param array $years The years to look for attended courses
 *
 * @return float
 */
public function getInternalPointsAttribute() : float
{
    return $this->attendedCourses()->external(false)->sum('points');
}

唯一的问题是年份过滤器.我希望我可以像第一个示例一样在调用访问器之前过滤User集合.

在这里做错了什么?

我目前正在使用此解决方法.这似乎很糟糕,因为我要重复很多代码.

/**
 * @param \Illuminate\Database\Eloquent\Builder $builder
 *
 * @param array                                 $years
 *
 * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
 */
public function scopeWithPoints(Builder $builder, array $years = [])
{
    # Join all columns
    $builder->join('user_roles', 'users.role_id', '=', 'user_roles.id')
            ->leftJoin('course_attendees', 'users.id', '=', 'course_attendees.user_id');

    # Join the course table for the years
    $builder->leftJoin('courses', function(JoinClause $join) use ($years) {
        # Join the courses table with year filters
        $join->on('course_attendees.course_id', '=', 'courses.id');

        # Apply the filters if available
        !empty($years) and $join->whereIn(DB::raw('YEAR(courses.end_date)'), $years);
    });

    # Select the columns
    $builder->select('users.*')->groupBy('users.id');

    # Sums
    $internalPoints = 'SUM(courses.points_internal)';
    $externalPoints = 'SUM(courses.points_external)';

    # Select the points
    $builder->selectRaw('COALESCE(' . $internalPoints . ', 0) as internal_points');
    $builder->selectRaw('COALESCE(' . $externalPoints . ', 0) as external_points');
    $builder->selectRaw('COALESCE(' . $internalPoints . ' + ' . $externalPoints . ', 0) as total_points');

    # Sum up the course points
    return $builder;
}

我的数据库结构的迁移可以在这里找到.

Schema::create('course_attendees', function(Blueprint $table)
{
    $table->integer('id', true);
    $table->integer('user_id')->index('course_attendees_users_id_fk');
    $table->integer('course_id')->index('course_attendees_courses_id_fk');
    $table->boolean('mijnafas');
});

Schema::create('courses', function(Blueprint $table)
{
    $table->integer('id', true);
    $table->string('title');
    $table->string('subject');
    $table->string('presenter');
    $table->date('start_date')->nullable()->comment('Set to not null later');
    $table->date('end_date')->nullable();
    $table->decimal('points', 4)->nullable();
    $table->string('location');
    $table->timestamps();
});

Schema::create('users', function(Blueprint $table)
{
    $table->integer('id', true);
    $table->string('first_name')->nullable();
    $table->string('last_name')->nullable();
    $table->timestamps();
});

Schema::table('course_attendees', function(Blueprint $table)
{
    $table->foreign('course_id', 'course_attendees_courses_id_fk')->references('id')->on('courses')->onUpdate('RESTRICT')->onDelete('RESTRICT');
    $table->foreign('user_id', 'course_attendees_users_id_fk')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
});

我注意到,仅在调用$test-> attendedCourses时,它会检索已过滤的集合.问题是我无法在此范围内应用.

问题

>为什么不对过滤后的集合求和?
>如何使它能够相应地过滤此集合?

解决方法:

问题在于,您的User类中的scopeAttendedCoursesInYears方法将范围限定在特定年份参加课程的用户,而不是范围限定在几年内进行的课程.

要执行您想要的操作,可以在关系中添加一个参数以过滤掉数据透视表而不是用户表:

public function attendedCourses(array $years = null)
{
    $query = $this->belongsToMany(Course::class, 'course_user');

    if ($years) {
        $query->whereRaw('YEAR(end_date) IN (?)', [
            'years' => implode(',', $years)
        ]);
    }

    return $query;
}

然后,您可以实现以下结果:

$user = User::find(123);

$user->attendedCourses($years)->external(false)->sum('points');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。