首页 > 解决方案 > 使用 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 课:高级函数

标签: laravellaravel-5relationship

解决方案


如果您需要显示部分课程,您可以在控制器中添加类似的内容$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');
    }

推荐阅读