Can anyone reply me? please!
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.
Please or to participate in this conversation.