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

ashutoshme's avatar

I am not getting how to apply constraints on the "with" function on the child when using parent Model

Laravel 5.3: I have 3 tables "Courses" ,"Chapters" and "Topics" , and model with name "Course", "Chapter" and "Topic" respectively.

Course.php

class Course extends Model {

protected $table = "courses";
protected $primaryKey = "slug";
public $incrementing = false;

public function chapter() {
    return $this->hasMany('App\Chapter','course_id','id');
}

}

Chapter.php

class Chapter extends Model { protected $table = "chapters";

public function topic(){
    return $this->hasMany('App\Topic');
}
public function course(){
    return $this->belongsTo('App\Course');
}

}

Topic.php

class Topic extends Model {

protected $table = "topics";

public function chapter() {
    $this->belongsTo('App\Chapter');
}

}

I have one of the route defined as /study/{course_slug}/{chapter_no}/{topic_no}

Now suppose if the URL is /study/core_java/4/7

So if I fetch data as $data=Course::with('chapter')->find($course_slug);

then it will return the course having 'slug' value as core_java, but what if I want to apply the constraint that it'll return me the chapters only those who have 'chapter_no' value as 4.

0 likes
6 replies
ImeDa's avatar

I'm not quite sure, but u need something like this:

$data = Course::with('chapter.course') -> whereHas('chapter.course', function($query) use($course_slag) {
    $query -> where('course_slag', $course_slag);
}) -> get();
ederson's avatar

i think it's the same thing but i use a bit different code

$data = Course::with('chapter.course=> function($query) use($course_slag) {
    $query -> where('course_slag', $course_slag);
      }])-> get();

ashutoshme's avatar

That's I am doing, I want different I am using the code $course = Course::with('chapter')->find($course_slug); With this I'm getting all the chapters, which are of the given slug only, but I want more constraints. What I want is to filter the chapters also, according to chapter_no.

Schema::create('courses', function (Blueprint $table) {
        $table->increments('id');
        $table->text('name');
        $table->string('slug',30)->unique();
        $table->timestamps();
        $table->softDeletes();
    });
    
    Schema::create('chapters', function (Blueprint $table) {
        $table->increments('id');
        $table->text('name');
        $table->integer('course_id');
        $table->integer('chapter_no');
        $table->timestamps();
        $table->softDeletes();  
    });
    
    Schema::create('topics', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->text('name');
        $table->integer('chapter_id');
        $table->integer('topic_no');
        $table->timestamps();
        $table->softDeletes();  
    });

These are the schemas, for my tables Now after fetching the chapters which are of a particular 'slug', then I want to filter the chapters and find the chapter which have particular 'chapter_no'.

Tell me if you still do not understand....I'll try to give a live demo.

Please or to participate in this conversation.