4
4
4sadly2021-09-25 15:34:37
Laravel
4sadly, 2021-09-25 15:34:37

How to fix route conflict?

I want to get users by their current department:

Route::apiResource('staffs/{staff_id}/users', UserController::class);

And get a list of departments to which users have access
Route::apiResource('users/{user_id}/staffs', ?::class);

And get all departments
Route::apiResource('staffs', StaffController::class);


What controller should I use?
1 table has 2 columns user_id and staff_id
2 table has N columns, one of them is staff_id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kovalchuk, 2021-09-25
@mamut

apiResourceit makes sense only for and then only if you have index, store, show, update, destroy ( https://laravel.com/docs/8.x/controllers#actions-h... )

Route::apiResource('staffs', StaffController::class);

For users in their current department, if such a url, then I think __invokethe class will be suitable
Route::get('staffs/{staff_id}/users', UserStaffController::class);

And accordingly for departments at the user so
Route::get('users/{user_id}/staffs', StaffUserController::class);

But I recommend using filters, for example, to take a list of users who have a department /users?staff_id=1and a list of departments that have a user. /staffs?user_id=1
Then it will be quite simple for a full-fledged crud + there is a filter + it's easy to add filter parameters
Route::apiResource('users', UserController::class);
Route::apiResource('staffs', StaffController::class);

And you can write the filter yourself and use it (for example , spatie/laravel-query-builder )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question