1

I have

users
    id
    username

companies
    id

areas
    id

area_company
    id
    area_id      
    company_id

area_company_user
    id
    area_company_id      
    user_id

company_user
    id
    company_id
    user_id

area_user
    id
    area_id
    user_id

where

  • one user has 0 to many areas AND one area can have 0 to many users
  • one area can have 0 to many companies AND one company can have 0 to many areas
  • one company can have 0 to many users AND one user can have 0 to many companies
  • one area_company can have 1 to many users AND one user can have 0 to many area_company
  • area_company_user has attributes specific to that kind of user

Also, I'm structuring the routes in the following manner

  1. /users - all existing users
  2. /areas - all existing areas
  3. /companies - all existing companies
  4. /areas/{area}/companies - all existing companies in a specific area
  5. /users/{user}/companies - all existing companies from a specific user
  6. /companies/{company}/areas - all existing areas the company is in
  7. /areas/{area}/companies/{company}/users - all existing users from a company that exists in a specific area

For 1., 2. and 3. I'm creating controllers that follow the next pattern

  • AreaController with methods index(), create(), store(), show(), edit(), update() and destroy()
GET /areas, index() method,
GET /areas/create, create() method,
POST /areas, store() method,
GET /areas/{area}, show() method,
GET /areas/{area}/edit, edit() method,
PUT/PATCH /areas/{area}, update() method,
DELETE /areas/{area}, destroy() method.

There's now basically two cases left from that route list

  • Case 1: 4., 5. and 6.
  • Case 2: 7.

My question is, should I create new controllers for each case since I'd like to perform various actions in each? If yes, that'd mean, respectively

  • Case 1: AreaCompanyController, UserCompanyController and CompanyAreaController
  • Case 2: AreaCompanyUserController

Note: this was a helpful answer but didn't exactly address my concern.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
  • 1
    Nothing problematic there with your convention and controller naming (i.e. `AreaCompanyUserController `). I like it how it looks: it is easy readable and understandable. Regarding search mentioned, you can have separated SearchController where you can have various method programatically. – Tpojka Jan 07 '21 at 22:00

2 Answers2

2

You can see there is already something called nested resource in docs which can cover your cases 4,5,6. In docs there is PhotoCommentController which you've described that you're using it (question case 1). For question case 2 you can for example make model for pivot table and have it that way for route and controller. For example AreaCompanyUserController if I understand well, you have here area_company pivot table with association of user. So many users can be part of same area_company association. You can circumvent Area and Company models use with use of AreaCompany model that would be the pivot model. Knowing id of that pivot model you can easily get associating area, company and users.

class AreaCompany extends Pivot
{
    /** code here */
}

Having this, you can name your resource route /area-companies and /area-companies/{$areaCompany}/users to avoid triple nesting. I presume any of these decisions have own consequences like subdirectory planning for controllers, form request files, providers, sometimes even route files. Probably heard and read for thousand times, but important is to stick with consistent way of it. Consider what could be lesser technical debt in some potential upgrade.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
0

In order to simplify the debugging and maintenance process of the project it's better to define the controller for each case in case if anything goes wrong it's easy to debug and solve the problem so you will be having

AreaController , UserController , CompanyController
Francis Tito
  • 126
  • 1
  • 9
  • Sure that was implied in 1., 2. and 3. Are you saying I should just stick with these? If yes, how'd you sugest to deal with the other two cases? – Tiago Martins Peres Jan 07 '21 at 13:35
  • yes stick with that it is real help in debugging in big projects, and in the other two cases you can include functionalities in one of the base controller eg for a /users/{user}/companies means to deal with users but who are in the company so its Usercontroller or if functions are too independent just create UserCompanyController – Francis Tito Jan 07 '21 at 13:46
  • Right, that's what I was also thinking of. What about Case 2? – Tiago Martins Peres Jan 07 '21 at 13:47
  • as how it sounds, you need all users so means it's in Usercontroller with parameters company and area – Francis Tito Jan 07 '21 at 14:02
  • thing is they'll have attributes specific to that kind of user – Tiago Martins Peres Jan 07 '21 at 14:03