Неожиданный результат при фильтрации отношений с использованием нескольких областей Laravel

Я использую Laravel 5.6 и пытаюсь отфильтровать отношения внутри User модель. Пользователь может посещать курсы, которые имеют привязанные к ним баллы.

Пользователи могут зарабатывать эти очки, посещая курсы. Это BelongsToMany отношения.

Я пытался создать область в этом User модель, которая будет включать в себя только участие курсы в течение ряда лет.

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

В моем Course модель, у меня есть область, которая фильтрует по году, в котором курс был.

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

С этим attendedCoursesInYears Я надеялся, что затем смогу рассчитать количество баллов для каждого пользователя, суммируя баллы курса, используя другие области на Course модель.

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 он получает отфильтрованную коллекцию. Проблема в том, что я не могу применить области действия для этого.

Вопросы

  • Как получилось, что он не будет суммировать отфильтрованную коллекцию?
  • Как я могу сделать так, чтобы он соответствующим образом отфильтровал эту коллекцию?

3

Решение

Проблема в том, что ваш 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');
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector