当前位置: 首页>>代码示例>>PHP>>正文


PHP Course::whereHas方法代码示例

本文整理汇总了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);
 }
开发者ID:dnvmbr,项目名称:p4,代码行数:39,代码来源:DashController.php


注:本文中的app\Course::whereHas方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。