Laravel学习管理系统的雄辩关系

时间:2019-06-17 10:15:44

标签: laravel laravel-5 relationship

我为LMS提供了三个模型,分别是课程,部分和课程。因此,在课程中当然有各节,在各节中有特定的课程。我已经有了课程和科室的关系,但是我的问题在课程上。

我的模型架构

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');
    }
}

Lesson.php

class Lesson extends Model
{
    public function section()
    {
        return $this->belongsTo(Section::class);
    }
}

Course.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
    ]);

}

Blade.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节:高级公式和函数           第三课:公式           第4课:高级功能

1 个答案:

答案 0 :(得分:0)

如果需要显示各节的内容,可以在控制器$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

Section.php

public function course(){
        return $this->belongsTo(Course::class);
    }
public function lessons(){
        return $this->hasMany(Lesson::class);
    }

Lesson.php

public function section(){
        return $this->belongsTo(Section::class, 'section_id');
    }