Best practice for CRUD process to manage many to many relations such as product-categories
What would basic process look like?
I would have some checkbox list with categories id. I would select those. How would i then save those relations, and update(if needed) on update operation?
Is there some common practice that doesn't involve disecting input manually and separating categories from array input(categories[])?
@Cvetan Using resourceful controllers, you might have a ProductCategoryController, with an update method that looks like this:
class ProductCategoryController extends Controller
{
public function update(Request $requst, Product $product)
{
$product->categories()->sync($request->input('category'));
}
}
@Cvetan You can use the same code when updating a product:
class ProductController extends Controller
{
public function update(UpdateProductRequest $request, Product $product)
{
$product->update($request->all());
$product->categories()->sync($request->input('category'));
// Redirect back to edit form or something
}
}