Aipo
Aipo

Reputation: 1985

Correct routes order in Laravel8 when adding attributes to Product using apiResource?

What is correct order for apiResource routes.

Route::apiResource('product/{product}/product_attribute', ProductAttributeController::class);
Route::apiResource('product', ProductController::class);

Or :

Route::apiResource('product', ProductController::class);
Route::apiResource('product/{product}/product_attribute', ProductAttributeController::class);

In my ProductAttributeControler I use:

public function store(Product $product, ProductAttributeRequest $request)

I added hasMany(ProductAttribute::class) to Product and belongsTo(Product::class) to ProductAttribute.

Upvotes: 0

Views: 129

Answers (1)

Peppermintology
Peppermintology

Reputation: 10210

The correct order is which ever order doesn't result in route conflicts. In your example you could do either and they should not conflict with each other.

I would be inclined to define them using your second option:

Route::apiResource('product', ProductController::class);
Route::apiResource('product/{product}/product_attribute', ProductAttributeController::class);

Reasoning being the longer, nested route definition follows the more outer non nested route definition.

Upvotes: 1

Related Questions