Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nurularifin's avatar

How to Display Data from Detail Table Base On ID each Row Laravel

Before I start, this is my first question and I'm not a native speaker, forgive me if my question makes you all confused.

I Have three tables, the table is Courses, Sections, and Syllabuses. Each table had a relationship. Courses table relation to Sections and Section relation to Syllabuses. The table look like this:

#Courses Table

    {
        Schema::create('courses', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedBigInteger('category_id')->default('1');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade')->onUpdate('cascade');
            $table->string('title');
            $table->string('status')->default('publish');
            $table->text('content')->nullable();
            $table->string('level');
            $table->date('start_at');
            $table->date('ended_at');
            $table->integer('capacity');
            $table->text('image')->nullable();
            $table->text('url')->nullable();
            $table->text('requirement')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
}```

#Sections Table

```public function up()
    {
        Schema::create('sections', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('course_id');
            $table->foreign('course_id')->references('id')->on('courses');
            $table->string('name');
            $table->timestamps();
        });
}```

#Syllabuses Table

```public function up()
    {
        Schema::create('syllabuses', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('section_id');
            $table->foreign('section_id')->references('id')->on('sections')->onDelete('cascade')->onUpdate('cascade');
            $table->string('title')->nullable();
            $table->text('content')->nullable();
            $table->text('description')->nullable();
            $table->string('total_of_sessions')->nullable();
            $table->string('durations')->nullable();
            $table->timestamps();
        });
}```

The following is each model from that table:
#Course Model

```class Course extends Model
{
    use HasFactory,SoftDeletes;

    protected $fillable = [
        'title',
        'content',
        'level',
        'start_at',
        'ended_at',
        'capacity',
        'image',
        'url',
        'requirement'
    ];

    protected $dates = ['deleted_at'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function sections()
    {
        return $this->hasMany(Section::class);
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }}```

#Section Model

```class Section extends Model
{
    use HasFactory;

    protected $fillable = ['name'];

    public function syllabuses()
    {
        return $this->hasMany(Syllabus::class);
    }

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

#Syllabus Model

```class Syllabus extends Model
{
    use HasFactory;

    public $table = 'syllabuses';

    protected $fillable = [
        'title',
        'content',
        'description',
        'total_of_sessions',
        'durations'
    ];

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

This is my controller code:

```//Display single course page

    public function showcourse(Request $request)
    {
        $syllabuses = Syllabus::where('section_id',[$request->idsection])->get();
        $sections   = Section::where('course_id',$request->route('course'))->get();
        $course     = Course::findOrFail($request->route('course'));
        return view('course-single', [
            'course' => $course,
            'sections' => $sections,
            'syllabuses' => $syllabuses
            ]);
    }```

From the above code, I have successfully displayed all course data and also section data base on course_id. My question is how to display the Syllabuses data base on Sections ID. I mean in this case, I want to display syllabuses or Course content like UDEMY.  As you all know each content there is a title like INTRODUCTION and they have SUB like COURSE INTRODUCTION and so on..., Right now I'm confused about how to display SUB based on sections' ID each row. Please anyone who can help me much much appreciate.
0 likes
2 replies
webrobert's avatar
Level 51

@nurularifin, you can use the relationships to nest them together...

public function showcourse(Request $request)
{
//    $syllabuses = Syllabus::where('section_id',[$request->idsection])->get();
//    $sections   = Section::where('course_id',$request->route('course'))->get();
    $course     = Course::with('sections.syllabuses')->findOrFail($request->route('course'));
    
    dd($course->toArray()); // have a look
    
    return view('course-single', [
        'course' => $course,
//        'sections' => $sections,
//        'syllabuses' => $syllabuses
    ]);
}
1 like
nurularifin's avatar

@webrobert Thanku so much, this worked right now, I just put this code:

@foreach($course->sections as $section)
     @foreach($section->syllabuses as $syllabus)
          // Here you will have syllabuses for individual sections
     @endforeach
@endforeach
2 likes

Please or to participate in this conversation.