lundi 17 juin 2019

Eloquent Relationship for Learning Management System using Laravel

I have three model for the LMS which are Course, Section and Lesson. So under course there are sections and under sections there are specific lessons. I already got the relationship for Course and Section but my problem is on the lessons.

My Model schema

Courses ID, title, user_id, cat_id, Sections Id, course_id, title, lessons id, section_id, course_id, user_id, body_content, lesson_title

I have tried this code but it doesnt work.. Here is my section model:

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">
                  
                  </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></button>
                        </div>
                  @endforeach    

                  @endforeach  


I need to come up with this output:

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire