Example of Database Transactions in themsaid/ergodnc

File app/Http/Controllers/OfficeController.php (link to Github)
use App\Http\Resources\OfficeResource;
use App\Models\Office;
use App\Models\User;
use App\Models\Validators\OfficeValidator;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Notifications\OfficePendingApproval;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Notification;

class OfficeController extends Controller
{
    //
    public function create(): JsonResource
    {
        abort_unless(auth()->user()->tokenCan('office.create'),
            Response::HTTP_FORBIDDEN
        );

        $attributes = (new OfficeValidator())->validate(
            $office = new Office(),
            request()->all()
        );

        $attributes['approval_status'] = Office::APPROVAL_PENDING;
        $attributes['user_id'] = auth()->id();

        $office = DB::transaction(function () use ($office, $attributes) {
            $office->fill(
                Arr::except($attributes, ['tags'])
            )->save();

            if (isset($attributes['tags'])) {
                $office->tags()->attach($attributes['tags']);
            }

            return $office;
        });

        Notification::send(User::where('is_admin', true)->get(), new OfficePendingApproval($office));

        return OfficeResource::make(
            $office->load(['images', 'tags', 'user'])
        );
    }
    //
}