Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iwanttolearnlaravel's avatar

Laravel: routing for CRUD multi entities

There are 3 entities:

Categories

Subcategories

Photos

(3 tables, 3 controllers, 3 models).

We need to implement CRUD operations for each entity. How to implement routing?

For example, to delete a subcategory, you need to make a DELETE request to:

categories / {category_id} / subcategories/{subcategory_id}

In the blade in href or in form action you need to write the following, for example:

{{ route('subcategories.destroy', [$category->id, $subcategory->id])}

But I don't need $category_id to delete a subcategory, just id subcategory is enough.

If then the same thing is implemented for photo, then you have to pass 3 parameters, 2 of which I do not need, but only the id of the photo.

I think I'm going some wrong way, can someone tell me how to implement routing and crud operations for categories and their subcategories?

routes/web.php:

// Category routes Route::resource('categories', 'CategoryController'); // Subcategories routes Route::prefix('categories/{category_id}')->group(function (){ Route::resource('subcategories', 'SubcategoryController'); }); SubcategoryController@destroy:

public function destroy($category_id, $subcategory_id) { $subcategory = Subcategory::findOrFail($subcategory_id); $subcategory->delete(); return redirect('/categories/' . $subcategory->category_id); }

0 likes
0 replies

Please or to participate in this conversation.