Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Finishing CRUD: Update, Delete and Resource Controller

Premium
6:46

In this lesson, we will finalize our resource Controller with two missing methods: update and delete the category.


First, the update method. The method for the Route is PUT, and in the Controller, the method is usually update.

routes/api.php:

Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
 
Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index']);
Route::get('categories/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show']);
Route::post('categories', [\App\Http\Controllers\Api\CategoryController::class, 'store']);
Route::put('categories/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'update']);
 
Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index']);

app/Http/Controllers/Api/CategoryController.php:

class CategoryController extends Controller
{
// ...
 
public function update(Category $category, StoreCategoryRequest $request)
{
$category->update($request->all());
 
return new CategoryResource($category);
}
}

In the Controller, we use a Route Model Binding to find a record and the same Form Request for validation. In the update method, we update the category and return the updated category.

In the client, we can send...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

BM
Briere Mostafa Amine ✓ Link copied!

Thank you.

��
Сергій Каліш ✓ Link copied!

when updating and creating data, I would not write : request->all(), but would write the validated data: request->safe()

BM
Briere Mostafa Amine ✓ Link copied!

or request->validated()

AO
Awab Omer ✓ Link copied!

Delete does not work on categories that has products linked to it and throws "Integrity constraint violation: 19 FOREIGN KEY constraint failed" the migration needs to have onDelete

$table->foreignId('category_id')->constrained()->onDelete('cascade');

N
Nerijus ✓ Link copied!

In theory you wouldn't want to delete products as they would be sold. Also, there's a helper cascadeOnDelete()

UH
Udara Herath ✓ Link copied!

On cascade delete we can do something like this, please let me know is there any easiest way

public function destroy(Category $category)
{
	if ($category->products()->exists()) {
		return response()->json([
				'message' => 'Cannot delete category with associated products'
		], 422);
	}

	$category->delete();
	return response()->noContent();
}
M
Modestas ✓ Link copied!

As far as I'm aware - this is the most common way to deal with this issue. So I don't think that there's a better way

M
M ✓ Link copied!

Would moving this category product check to a DeleteCategoryFormRequest be consistent with StoreCategoryRequest?