Monday, 12 November 2018

Unexpected result on relationship filtering using multiple Laravel scopes

I'm using Laravel 5.6 and I'm trying to filter a relationship inside my User model. A user can attend courses, which has points bound to them.

Users can earn these points by attending the courses. This is a BelongsToMany relationship.

I'm tried creating a scope in this User model that would only include the attended courses in a range of years.

/**
 * 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]);
}

In my Course model, I have a scope that filters on the year that course was in.

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

With this attendedCoursesInYears scope I hoped I could then calculate the amount of points for each user by summing up the course points, using other scopes on the Course model.

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

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

In my CourseCategory modal, the scope looks like this:

/**
 * 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');
}

For calculating this, I tried doing something like this.

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

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

This however returned the total sum of all courses.

The use of a scope is the only option here, as I can see. I want to create custom attributes from these values using accessors like this. This so I could easily sort the calculated values.

/**
 * 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');
}

The only problem here is the year filter. I was hoping I could filter the User collection before calling the accessor like my first example.

What am I doing wrong here?

I noticed when just calling $test->attendedCourses it retrieves the filtered collection. The problem with this is that I can't apply the scopes on this.

Questions

  • How does it come that it will not sum the filtered collection?
  • How can I make it so that it will filter this collection accordingly?


from Unexpected result on relationship filtering using multiple Laravel scopes

No comments:

Post a Comment