0

I have a many-many relationship in my Laravel application, Courses and Users, and users can enroll in a course with my Enrollments table. My Enrollments table functions as the many-many table for Courses and Users. A user can enroll in a course one time.

My idea is that Enrollments will function more than just a many-many table, and store info like the payment amount, expiration date of the enrollment, etc. However, in Laravel Nova (my admin dashboard), I can't make the HasOne:: relationship work for the Enrollments table. It's just ugly IDs that I never want shown. Help.

G.S.
  • 502
  • 8
  • 16
  • If it's a many-to-many relationship, why are using the `HasOne` relationship? are you trying to make a relationship between the pivot table/model and another table? If not, just add additional pivot attributes like the [documentation](https://laravel.com/docs/eloquent-relationships#many-to-many) state. Additional, the respective method to be used in Nova for a `m-m` relationship is the [BelongToMany](https://nova.laravel.com/docs/1.0/resources/relationships.html#belongstomany). – Kenny Horna Nov 01 '18 at 02:05
  • For the Course and Enrollments tables, it is an `BelongsTo` relationship of course. I am saying that the `HasOne` should be for the Courses and Users entries individually... – G.S. Nov 01 '18 at 02:14
  • `Courses | Users` is many-many (BelongsTo on each), `Enrollments` is the storage table for the many-many table. My question is how to do `Enrollments` in reverse, so I can just do `$enrollment->course` or `$enrollment->user` instead of getting the ugly ID. – G.S. Nov 01 '18 at 02:17
  • Just define the [reverse of the relationships](https://laravel.com/docs/5.7/eloquent-relationships#one-to-many-inverse) in the `Enrollment` model and use [this](https://nova.laravel.com/docs/1.0/resources/relationships.html#belongsto) in Nova. – Kenny Horna Nov 01 '18 at 02:20

0 Answers0