Get the collection instead of the ID

  eloquent, laravel, many-to-many, one-to-many, php

I have a relationship between three tables (Courses , Professors , Hours) I created a 4th table called course_hour_professors to make the relation and I made it relationship between the 4 tables because I know that if I get a pivot table with more than two FKs, I have to treat it as full model. So instead of Many-to-Many relationship. I have to divide it into multiple of One-to-Many relationships. all of this is working now but Professors table is related to User table and I need to access the professor name which is located in the User table since it’s related to it but I can only access the User_id from my course_hour_professors as shown below :

  {
        "id": 3,
        "professor_id": 94,
        "course_id": 132,
        "hour_id": 1,
        "nbr_hour_course": 9,
        "course": {
            "id": 132,
            "created_at": "2018-07-25T16:02:19.000000Z",
            "updated_at": "2018-09-07T07:38:01.000000Z",
            "name": "Algèbre - 1"
        },
        "professor": {
            "id": 94,
            "created_at": "2018-09-24T08:18:30.000000Z",
            "updated_at": "2019-10-23T14:32:00.000000Z",
            "user_id": 810,
            "grade": "Maître assistant",
            "cin_delivered_at": "le 20/05/2015",
            "cnrps": "0034966476",
            "cnss": "00000000",
            "phone": "20980961"
        },
        "hour": {
            "id": 1,
            "heure_suivie": 13,
            "type": "jour",
            "created_at": "2021-08-19T20:02:16.000000Z",
            "updated_at": "2021-08-05T20:02:16.000000Z",
            "week_id": 1
        }
    }

Professor Model :

class professor extends Model
{

    public function user()
    {
        return $this->belongsTo("AppUser",'user_id','id');
    }

    public function courses()
    {
        return $this->belongsToMany('Appcourse');
    }

    public function hours()
    {
        return $this->belongsToMany('AppHour');
    }

    public function courseHourProfessor()
    {
        return $this->hasMany('AppCourseHourProfessor', 'professor_id','id');
    }
}

CourseHourProfessor Model :

class CourseHourProfessor extends Model
{
    public function professor()
    {
        return $this->belongsTo('Appprofessor', 'professor_id');
    }

    public function course()
    {
        return $this->belongsTo('Appcourse', 'course_id');
    }

    public function hour()
    {
        return $this->belongsTo('AppHour', 'hour_id');
    }
}

Course Model

class Course extends Model
{
  public function professor()
    {
        return $this->belongsToMany('Appprofessor');
    }

  public function hours()
    {
        return $this->belongsToMany('AppHour');
    }

    public function courseHourProfessor()
    {
        return $this->hasMany('AppCourseHourProfessor', 'course_id','id');
    }
}

Hour Model :

class Hour extends Model
{
    //
    public function week()
    {
        return $this->belongsTo(Week::class);
    }

    public function courses()
    {
        return $this->belongsToMany('Appcourse');
    }

    public function professors()
    {
        return $this->belongsToMany('Appprofessor');
    }

    public function courseHourProfessor()
    {
        return $this->hasMany('AppCourseHourProfessor', 'hour_id','id');
    }
}

CourseHourProfessor Controller :

public function getById($id)
     {
        return CourseHourProfessor::with(['course','professor','hour'])->where('id', $id)->get();
     }

I want to know if it’s possible for the result to be like this

 {
        "id": 3,
        "professor_id": 94,
        "course_id": 132,
        "hour_id": 1,
        "nbr_hour_course": 9,
        "course": {
            "id": 132,
            "created_at": "2018-07-25T16:02:19.000000Z",
            "updated_at": "2018-09-07T07:38:01.000000Z",
            "name": "Algèbre - 1"
        },
        "professor": {
            "id": 94,
            "created_at": "2018-09-24T08:18:30.000000Z",
            "updated_at": "2019-10-23T14:32:00.000000Z",
            "user_id": 810,
            "grade": "Maître assistant",
            "cin_delivered_at": "le 20/05/2015",
            "cnrps": "0034966476",
            "cnss": "00000000",
            "phone": "20980961"
            "user": {
                "id": 60,
                "email": "[email protected]",
                "name": "Aymen",
                "lastname": "SELLAOUTI",
                "type": "professor",
                "birthday": "2018-01-01",
                "gender": "Male",
                "image_id": 34,
                "cin": "07075595",
                "fullname": "SELLAOUTI Aymen"
        }
        },
        "hour": {
            "id": 1,
            "heure_suivie": 13,
            "type": "jour",
            "created_at": "2021-08-19T20:02:16.000000Z",
            "updated_at": "2021-08-05T20:02:16.000000Z",
            "week_id": 1
        }
    }

Source: Laravel

Leave a Reply