1

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">
          {{$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  

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

Priyanka khullar
  • 509
  • 1
  • 5
  • 25

1 Answers1

0

If you need to show lessons of sections you can add something like this in your controller $course = Course::with(['sections', 'sections.lessons'])->find($id);.

Blade

@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

And do dd($course) to see what you got. I think you need that to present like this:

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');
    }
mare96
  • 3,749
  • 1
  • 16
  • 28
  • i cant figure it out.. what about in the blade view.. does my loop correct..if i'm going to add that in controller, how's that.. – Argie Dioquino Jun 17 '19 at 13:09
  • Can you show me your `dd($course)` and i will write code for blade in my answer. @Argie – mare96 Jun 17 '19 at 13:11
  • Course {#1100 ▼ #fillable: array:2 [▶] #connection: "mysql" #table: "courses" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:8 [▶] #original: array:8 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [▶] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] } – Argie Dioquino Jun 17 '19 at 13:20
  • Check update i think this will work. Just change in your controller as I mentioned in answer. – mare96 Jun 17 '19 at 13:25
  • already tried but the output is the same before.. The section 1 lessons were repeated also in section 2.. would it be the relationship from section to lessons.. do I need to use morphMany for sections in Course.php – Argie Dioquino Jun 18 '19 at 05:15
  • https://imgur.com/d6CAZfL Please see the link. The blue ones are sections and under sections those are lessons – Argie Dioquino Jun 18 '19 at 05:21
  • That is happening because some mistake in models. @Argie – mare96 Jun 18 '19 at 08:23