laravel - 使用 Laravel 的学习管理系统的雄辩关系
问题描述
我有三个 LMS 模型,即课程、部分和课程。所以在课程下面有部分,在部分下面有具体的课程。我已经有了 Course 和 Section 的关系,但我的问题出在课程上。
我的模型架构
Courses
ID,
title,
user_id,
cat_id,
Sections
Id,
course_id,
title,
lessons
id,
section_id,
course_id,
user_id,
body_content,
lesson_title
我已经尝试过这段代码,但它不起作用。这是我的部分模型:
class Section extends Model
{
public function course()
{
return $this->belongsTo(Course::class);
}
public function lessons()
{
return $this->hasMany(Lesson::class,'section_id');
}
}
课程.php
class Lesson extends Model
{
public function section()
{
return $this->belongsTo(Section::class);
}
}
课程.php
class Course extends Model
{
protected $fillable = ['title', 'image'];
public function user()
{
return $this->belongsTo(User::class);
}
public function sections()
{
return $this->hasMany(Section::class);
}
}
LmsController.php
public function show($id)
{
$course = Course::with('sections')->find($id);
$othercourses = Course::orderby('created_at','desc')->get();
$sections = Section::with('lessons')->find($id);
$previous = Course::where('id', '<', $course->id)->orderBy('id', 'desc')->first();
$next = Course::where('id', '>', $course->id)->first();
$categories = Lmscategory::orderBy('name', 'asc')->get();
return view('users.learning.show', [
'course'=> $course,
'othercourses'=>$othercourses,
'previous'=>$previous,
'next'=>$next,
'categories'=>$categories,
'sections'=>$sections
]);
}
刀片.php
@foreach($course->sections as $section)
<button type="butcon" class="list-group-item list-group-item-action active">
{{$section->title}}
</button>
@foreach($sections->lessons as $lesson)
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action">
<i class="fa fa-check-square-o"> </i>{{$lesson->lesson_title}}
</button>
</div>
@endforeach
@endforeach
我需要想出这个输出:
课程名称:ICT应用软件
第 1 节:了解函数第 1 课:函数向导第 2 课:IF 函数第 2 节:高级公式和函数第 3 课:公式第 4 课:高级函数
解决方案
如果您需要显示部分课程,您可以在控制器中添加类似的内容$course = Course::with(['sections', 'sections.lessons'])->find($id);
。
刀
@foreach($course->sections as $section)
<button type="butcon" class="list-group-item list-group-item-action active">
{{$section->title}}
</button>
@foreach($section->lessons as $lesson)
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action">
<i class="fa fa-check-square-o"> </i>{{$lesson->lesson_title}}</button>
</div>
@endforeach
@endforeach
dd($course)
看看你得到了什么。我认为你需要这样呈现:
Course Title: ICT Application Software
Section 1: Getting to know function
Lesson 1: Function Wizard
Lesson 2: IF Function
Section 2: Advance formula and functions
Lesson 3: Formula
Lesson 4: Advance Functions
节.php
public function course(){
return $this->belongsTo(Course::class);
}
public function lessons(){
return $this->hasMany(Lesson::class);
}
课程.php
public function section(){
return $this->belongsTo(Section::class, 'section_id');
}
推荐阅读
- python - 发布数据时将 ForeignKey 设置为特定模型
- c - 多次扫描字符串并在每行中打印辅音数量
- android - 此 NavController 不知道导航目的地 xxx
- mysql - TypeORM 错误 - DataTypeNotSupportedError
- android - 在 Ubuntu 上使用颤振和 codemagic 构建和安装 ios 应用程序时出现问题
- css - 按页面更改菜单项类别不单击
- javascript - 在 AWS 上运行的 Node.JS 应用程序性能问题
- javascript - 使用 document.querySelectorAll 和 innerHTML 问题查找输入
- playframework - Play Framework - 在 CSRF 令牌不匹配时显示自定义错误
- android - 片段被无限创造