This would also give you the opportunity to add some data describing things like what the overall participant.status is, when they became associated with that project, when they left that project, or who their supervisor (parent_participant_id) might be. Then your query would look like this: Auth::user()->participants()->with()->get() Then your pivot table need only be a many-to-many between Participant and Role, and leave User out of it. I believe you need a new Participant table that has a many-to-one relationship with User, and a one-to-one relationship with Project. But you're also using it to represent the concept of a "Participant" of a project. So start by adding a many-to-many relation method to your Projects Model called roles, and run your query like this: app(Projects::class)->with('roles')->wherePivot('user_id', Auth::user()->getKey())->get()Īs for the structure, I think you have a little bit of a single-responsibility violation there. This can only be done if the Projects model is your entry point as opposed to your User. your desired datagram shows the project data to be top-level with nested 'roles'.). But what you're looking for is to go from the Project to the Role through that pivot table. And you can go from the User to the Role through your pivot table. So, you can go from the User to the Project through the pivot table. I'll get into some thoughts on that after actually answering your question though. There's a little awkwardness in your DB structure too, in that your pivot table is joining three tables instead of two. The problem is, the data from the pivot table isn't coming from a top-level Model class, so there isn't anything in the way of a relationship method for you to reference. You're essentially asking for an eager-load on a pivot table. Now I can easily get projects of authenticated user like that: $user = Auth::user() īut I would like to "inject" information about user role into response likat that: [ Return $this->belongsToMany('App\Library\Models\Project', 'project_user_role')->withPivot() My User, Project and Role models got defined belongsToMany relation like that: //User model example See image for screenshot from MySQL Workbench. Pivot table contains user_id, project_id and role_id columns. There is m:n relation between them so I have also pivot table called project_user_role. This is my show method and tried to achieve what i just described above.In my Laravel app I have three database tables called users, projects and roles. Return $this->belongsToMany(Business::class, "tblBusinessSubMenu", "inSubMenuId", "inBusinessId") Return $this->belongsTo(MainMenu::class, "inMainMenuId") 00:26 Basic example of pivot table 02:49 Customizing pivot table. SubMenu Model class SubMenu extends Model This is a deep dive into belongsToMany Relationships in Laravel with examples - from simple, to much more complex. Return $this->belongsToMany(Business::class, "tblBusinessMainMenu", "inMainMenuId", "inBusinessId") Return $this->hasMany(SubMenu::class, "inMainMenuId") MainMenu Model class MainMenu extends Model Pivot table contains userid, projectid and roleid columns. There is m:n relation between them so I have also pivot table called projectuserrole. Return $this->belongsToMany(MainMenu::class, "tblBusinessMainMenu", "inBusinessId", "inMainMenuId") Laravel Eloquent - querying pivot table Ask Question Asked 5 years ago Modified 5 years ago Viewed 27k times 2 in my Laravel app I have three database tables called users, projects and roles. Return $this->belongsToMany(SubMenu::class, "tblBusinessSubMenu", "inBusinessId", "inSubMenuId") I want to query all the mainMenus under a business (this can be done via pivot table) also i want to get all the subMenus under each of those mainMenus and also want to check if that subMenu has relationship with the business.īusiness Model class Business extends Model so this can be achieve with something like this Business::with("mainMenus.subMenus")->findOrFail($busiessId) īut i also want to check that if those subMenus under mainMenus has relationship with this particular business via a pivot table. I want to query all the mainMenus that belongs to a business and also i want to query all the subMenus belongs that mainMenus. MainMenu has many subMenus and subMenu belongs to a mainMenu. (has pivot table named tblBusinessSubMenu) (has pivot table named tblBusinessMainMenu)īusiness and subMenu has manyToMany relationship. business, mainMenu and subMenu.īusiness and mainMenu has manyToMany relationship.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |