Reputation: 359
Please I want to make link using id (Semesters)
this how is my DB :
Courses: id , title , pdf_file , extension , description , matieres_id and timetables
Matieres: id , matiere , semester_id and timetables
Semesters: id , semester, matieres_id and timetables
route/web.php
Route::get('/lms/semestre', 'HomeController@semestres');
Route::get('/lms/', 'HomeController@lms');
Route::get('/lms/matieres/{id}', 'HomeController@matieres');
Route::get('/lms/course/{id}', 'HomeController@course');
Route::get('/lms/courses', 'HomeController@courses');
Course.php (model)
class Course extends Model
{
use SoftDeletes;
protected $table = 'courses';
protected $fillable = ['id'];
public function matiere()
{
return $this->belongsTo(\App\Matiere::class);
}
}
Matiere.php (Model)
class Matiere extends Model
{
protected $table = 'matieres';
protected $fillable = ['id'];
public function semestre()
{
return $this->belongsTo(\App\Semestre::class);
}
}
Semestre.php (Model)
class Semestre extends Model
{
protected $table = 'semestres';
protected $fillable = ['id'];
public function matiere()
{
return $this->belongsTo(\App\Matiere::class);
}
}
My issue is how to make url /lms/courses/{semester_id}
and display a page where is courses list using course->matiere_id->semesters
.
It's very complicated for me and I don't want to create column semester_id
in course table.
Upvotes: 0
Views: 77
Reputation: 1052
Try this one.
Note: Make sure CourseDB
is created first before SemesterDB
database:
course_db:
Schema::create('courses', function (Blueprint $table) {
...
});
semester_db:
Schema::create('semesters', function (Blueprint $table) {
...
$table->unsignedBigInteger('course_id');
...
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
})
Model:
course_model:
protected $guarded = [];
public function semester() {
return $this->hasOne(Semester::class);
}
semester_mode:
protected $guarded = [];
public function course() {
return $this->belongsTo(Course::class);
}
controller:
App\Course;
public function index() {
$courses = Course::all();
dd($courses->semester);
}
Upvotes: 1