本文整理汇总了PHP中app\Course::whereHas方法的典型用法代码示例。如果您正苦于以下问题:PHP Course::whereHas方法的具体用法?PHP Course::whereHas怎么用?PHP Course::whereHas使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Course
的用法示例。
在下文中一共展示了Course::whereHas方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getIndex
public function getIndex(Request $request)
{
$userCourses = \App\Course::whereHas('users', function ($query) {
$query->where('user_id', '=', \Auth::id());
})->get();
# Get all the requirements
$requirements = \App\Requirement::all();
# Array to keep track of the requirements this user has completed
$satisfied_requirements = [];
# See what requirements this user has satisfied by looping through all their courses
foreach ($userCourses as $userCourse) {
# Find out what requirements this particular course satisifies
$course = \App\Course::where('id', '=', $userCourse->id)->with('requirements')->first();
# Loop through all the requirements this course satisfies
foreach ($course->requirements as $requirement) {
# If we've never had an instance of this requirement being satisfied
# We'll start the count off with how many hours this particular course satisfies
if (!isset($satisfied_requirements[$requirement->id]['course_hours'])) {
$satisfied_requirements[$requirement->id]['course_hours'] = $course->course_hours;
} else {
$satisfied_requirements[$requirement->id]['course_hours'] += $course->course_hours;
}
}
}
# Now we'll loop through all the requirements and update each one with a count of 'hours_remaining'
foreach ($requirements as &$requirement) {
# If they have anything that satisfies this requirement
if (isset($satisfied_requirements[$requirement->id])) {
# Then how many hours have been completed...
$hours_completed = $satisfied_requirements[$requirement->id]['course_hours'];
# Subtract that from this requirements `requirement_hours`
# That kets stored in a new attribute for this $requirement, called `remaining_hours`
$requirement->remaining_hours = $requirement->requirement_hours - $hours_completed;
} else {
$requirement->remaining_hours = $requirement->requirement_hours;
}
}
return view('dash.index')->with('userCourses', $userCourses)->with('requirements', $requirements);
}