Skip to main content

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

Read more here

Delete Post with Confirmation Modal

Premium
4 min read

Now that we can create and edit posts, let's make a delete post action, with the confirmation modal. For the confirmation modal, we will again use the sweetalert2.

delete modal confirmation


First, let's add a new destroy method in the PostController. Don't forget that we don't need to add any additional routes for this Controller methods, because all the routes come from the Route::apiResource() that we added earlier in the routes/api.php.

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

class PostController extends Controller
{
// ...
public function destroy(Post $post)
{
$post->delete();
 
return response()->noContent();
}
}

What to return in this method? It is an open question. It's a common practice to return nothing because the record is deleted. That response()->noContent() would return 204 HTTP Status Code.

Now in the posts Composable, we need to add a new...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

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

Already a member? Login here

Comments & Discussion

M
marcelo-kazalukian ✓ Link copied!

Great course so far.

I have a question, in the file resources/js/composables/posts.js in the deletePost method, Why did you do: router.push({ name: 'posts.index' }) if we are in that page? I commented that line and it still working. Is there any special reason? thanks

M
Modestas ✓ Link copied!

This reload the page and refreshes the table without the deleted row.

M
marcelo-kazalukian ✓ Link copied!

I thought the page was refreshed by getPosts()