Reputation: 41
How can i get Users->Projects with its ProjectTypes relation, where Users is connected by ManyToMany with ProjectTypes.
I have this tables:
User
- id
- name
Project
- id
- name
ProjectType
- id
- name
project_type_user
- id
- user_id
- project_type_id
project_user
- id
- user_id
- project_id
Now i have this table:
auth()->user()->projectTypes
id type name project name
$type->id $type->name $type->project->name
But i want this table:
auth()->user()->projects with projectTypes:
id project name project types
$project->id $project->name here iterating over multiple projectsTypes
The issue is that when i call auth()->user()->projectTypes
i'm getting multiple projectTypes
that have one project, because that i want to get projects with multiple projectTypes
which are connected by many to many with users
Upvotes: 0
Views: 129
Reputation: 324
I think I know what you are trying to do and will be building my answer on it. Your structure is fundamentally incorrect if you are trying to list projects with their (multiple) types. Because right now, you have no connection at all between projects and project_types. Therefore, you may need to change the migrations to be:
User
- id
- name
Project
- id
- name
ProjectType
- id
- name
- project_id
project_type_user (DELETE THIS)
- id
- user_id
- project_type_id
project_project_type (NEW)
- id
- project_id
- project_type_id
project_user
- id
- user_id
- project_id
What changed was that we cut the direct connection between project_types and users because we can keep the projects as the bridge. Now you can easily use the projectTypes()
relationship you'll make in the Projects model like:
$type->project->projectTypes;
Let me know if I completely misinterpreted your structure's purpose.
Upvotes: 1