I have a url that looks like this
/courses/{slug}/units/{unit_id}
This page shows unit content of this specific course
What I am doing is : I added Next and Previous buttons for users to navigate between this specific course units.
The problem [ Users can keep clicking next next next going through al the Units to even other courses
in my Course.php
public function getRouteKeyName(){
return 'slug';
}
in my UnitsController :
public function show(Course $course, Unit $unit )
{
$course = Course::find($course);
$previous_record = Unit::where('id', '<', $unit->id)->where('course_id', 1) ->orderBy('id','desc')->first();
$next_record = Unit::where('id', '>', $unit->id)->where('course_id', 1)->orderBy('id')->first();
dd($previous_record);
return view('courses.units.show', compact('course', 'unit', 'previous_record','next_record'));
}
Notice Here : I was trying to just see if it will work by adding 1 for the course ID and it worked perfectly .. so what I need to do here is to replace this 1 with the a variable that gets course ID .. because when I use anything, I get error undefined variable.
$previous_record = Unit::where('id', '<', $unit->id)->where('course_id', 1) ->orderBy('id','desc')->first();
$next_record = Unit::where('id', '>', $unit->id)->where('course_id', 1)->orderBy('id')->first();
Course has many units
Unit belongsTo course
Unit table has "course_id" column.
Would appreciate any input :)