Skip to content
Advertisement

Get user owned courses with a Laravel relationship

I need to get all the courses that the user owns with a for each loop in my view. I tried to set up a relationship that way: User.php

JavaScript

Course.php

JavaScript

User_Course.php

JavaScript

dashboard.blade.php

JavaScript

This is what the dd($course) returns:

JavaScript

Im trying to get each course detail via the course_id. for example if a user with the id of 3 owns a course with the course_id of 2, the database row will look like this: id|user_id|course_id 1 | 3 | 2

If you need more code, tell me in the comments.

users migration:

JavaScript

courses migration:

JavaScript

user__courses migration:

JavaScript

Advertisement

Answer

You can do the following: inside user model you need to create this function

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement