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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Route model binding lets the framework resolve and inject the target record before your logic runs.
- 2Checking trashed state before restoring turns an invalid action into a clear 409 instead of a silent no-op.
- 3Returning fresh model data confirms the mutation actually took effect in the response.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
go
package middleware import ( "net/http"
Per-plan export limits in Gin middleware
middleware
rate-limiting
authorization
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-soft-delete-restore-endpoint-works-in-laravel-explained-php-208d/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.