PHP code example of ysm / with-transaction

1. Go to this page and download the library: Download ysm/with-transaction library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ysm / with-transaction example snippets


use App\Traits\WithTransaction;

class Post extends Model
{
    use WithTransaction;

    protected $fillable = ['title', 'content'];
}

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function store(Request $request)
    {
       $post = Post::transaction(function () use ($request) {

            $model = Post::create($request->validated());

            $model->tags()->attach(Tag::inRandomOrder()->take(3)->pluck('id'));
            $model->categories()->attach(Category::inRandomOrder()->take(3)->pluck('id'));

            $model->addMediaFromRequest('image')
                ->toMediaCollection('posts');

            // all or nothing rollback automatically
            return $model;
        });

        return response()->json([
            'message' => 'Post created successfully',
            'post' => $post,
        ]);
    }
}

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function Update(Request $request, Post $post)
    {
        $post = Post::transaction(function () use ($post) {

            $post->update([
                'title' => 'Update title Post v1',
            ]);

            $post->tags()->sync([4, 5, 6]);

            return $post;
       });

        return response()->json([
            'message' => 'Post updated successfully',
            'post' => $post,
        ], 200);
    }
}

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
    public function destroy(Post $post)
    {
        $post = Post::transaction(function () use ($post) {
            $post->delete();
            // If you want to perform any additional operations after deletion,
            // you can do so here. For example, logging or cleaning up related data.
            // If the deletion fails, the transaction will automatically roll back.
            return $post;
        });

        return response()->json([
            'message' => 'Post deleted successfully',
            'post' => $post,
        ], 200);
    }
}

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
use YSM\Support\transaction;

class PostController extends Controller
{
    public function destroy(Post $post)
    {

        $post = transaction(function () use ($post) {
            $post->delete();
            // If you want to perform any additional operations after deletion,
            // you can do so here. For example, logging or cleaning up related data.
            // If the deletion fails, the transaction will automatically roll back.
            return $post;
        });

        return response()->json([
            'message' => 'Post deleted successfully',
            'post' => $post,
        ], 200);
    }
}

use function YSM\Support\transaction;

transaction(function () use ($post) {
    $post->update([...]);
}, attempts: 2, onSuccess: fn () => Log::info('Done!'), onFailure: fn ($e) => report($e));

use YSM\Support\Facades\Transaction;

Transaction::start()
    ->attempts(3)
    ->onSuccess(fn ($result) => Log::info('Transaction success', ['id' => $result?->id]))
    ->onFailure(fn ($e) => Log::error('Transaction failed', ['message' => $e->getMessage()]))
    ->run(fn () => Post::create([...]));