У меня есть небольшая трудность в отношениях Laravel. Я установил все отношения между CourseModel, CourseTimeModel и CourseInstructorModel. Но проблема в том, что я хочу получить имя_инструктора из таблицы инструкторов. Возможно ли, что я могу получить имя инструктора из таблицы инструктора, используя отношения?Laravel Relationship
CourseModel.php
class CourseModel extends Model {
protected $table = 'course';
protected $fillable = [
'course_name',
'fee',
'duration',
'description',
'created_at',
'updated_at',
];
public function courseInstructor() {
return $this->hasMany('App\CourseInstructorModel');
}
public function courseTime() {
return $this->hasMany('App\CourseTimeModel');
}
public function getCourses() {
$courses = $this->with(['courseTime', 'courseInstructor'])->paginate(10);
return $courses;
}
}
CourseInstructorModel.php
class CourseInstructorModel extends Model
{
protected $table = 'course_instructor';
protected $fillable = [
'course_id',
'instructor_id',
'created_at',
'updated_at',
];
public function course()
{
return $this->belongsTo('App\CourseModel');
}
}
CourseTimeModel.php
class CourseTimeModel extends Model
{
protected $table = 'course_time';
protected $fillable = [
'course_time',
'course_id',
'created_at',
'updated_at',
];
public function course()
{
return $this->belongsTo('App\CourseModel');
}
}
Где ошибка? Где вы пытаетесь использовать свои отношения? –
Я пытаюсь получить все курсы через CourseModel @ getCourses(), используя отношения. –
$ course = CourseModel :: find (n); $ course_instructors = $ course-> courseInstructor; $ course_times = $ course-> courseTime() -> paginate (10); –