# Wrap a Vue save in a transaction

> DB::transaction commits the post and its relations together. Redirect after the closure returns.

- URL: https://laravel-vuejs.space/laravel-db-transaction-vue
- Author: Anass Ez-zouaine (https://laravel-vuejs.space/author/anass-ez-zouaine)
- Published: 2026-10-11
- Topics: Laravel, VueJs

People searching “Laravel transaction Vue” open a transaction and redirect inside it. The redirect throws, and the commit depends on how the exception is handled. Do the writes in the closure. Redirect after it returns. The [CRUD guide](https://laravel-vuejs.space/laravel-inertia-vue-crud) is the single-table version of this save.

> **TL;DR**
>
> -   DB::transaction commits the post and its relations together.
> -   Redirect after the closure returns.
> -   Do not catch `\Throwable` inside the closure and return a Vue error string.
> -   Do not start a transaction in the Vue page.
>
> **Who it's for:** Laravel + Vue developers
>
> **Applies to:** Laravel 13 · Vue 3

## The fix

```php
$post = DB::transaction(function () use ($request, $validated) {
    $post = Post::create([
        'title' => $validated['title'],
        'user_id' => $request->user()->id,
    ]);

    $post->tags()->sync($validated['tag_ids'] ?? []);

    return $post;
});

return to_route('posts.show', $post);
```

If `sync` throws, the post is not left behind. Validation stays outside the transaction. You do not hold a lock while you debate the request.

## What to avoid

> **Gotcha**
>
> Do not catch `\Throwable` inside the closure and return a Vue error string. Let it roll back. Send a field error only when you know the failure is the user’s input. Do not start a transaction in the Vue page.

## FAQ

**Q: How do I wrap a Vue save in a transaction?**

DB::transaction commits the post and its relations together. Redirect after the closure returns.

**Q: What goes wrong with Laravel transaction Vue?**

Many developers open a transaction and redirect inside it. Do not catch `\Throwable` inside the closure and return a Vue error string.
