php 28 lines · 5 steps

How a soft-delete restore endpoint works in Laravel

A single-action controller authorizes, guards, and undeletes a soft-deleted Article.

Explained by highlit
1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Models\Article;
6use Illuminate\Http\JsonResponse;
7use Illuminate\Support\Facades\Gate;
8 
9class ArticleRestoreController extends Controller
10{
11 public function __invoke(Article $article): JsonResponse
12 {
13 Gate::authorize('restore', $article);
14 
15 if (! $article->trashed()) {
16 return response()->json([
17 'message' => 'Article is not deleted.',
18 ], 409);
19 }
20 
21 $article->restore();
22 
23 return response()->json([
24 'message' => 'Article restored.',
25 'data' => $article->fresh(),
26 ]);
27 }
28}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Route model binding lets the framework resolve and inject the target record before your logic runs.
  2. 2Checking trashed state before restoring turns an invalid action into a clear 409 instead of a silent no-op.
  3. 3Returning fresh model data confirms the mutation actually took effect in the response.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How a soft-delete restore endpoint works in Laravel — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code