#Best way to rollback or use transaction

9 messages · Page 1 of 1 (latest)

faint forge
#

I am creating a system which create 2 things. An organization in the organization table and a profile in the profile table.
The point would be that when you create an organization, it automatically creates you a profile.
So I have a route that creates an organization and one that create a profile. Should I call my route inside a transaction, should I call the static function instead of the route.
I am kinda lost and I wanna make sure that if one save() gets an error, everything is reversed. I read about transaction and rollback and I'm not really sure how to go

public function Create(Request $request)
{
    $validatedData = $request->validate([
        'address' => ['required', 'max:255'],
    ]);

    $org = new Organization();
    $org->address = $validatedData['address'];
    $org->save();
    // Create the profile would be here, be either calling the route or the function that is called by the route
}

Here is my orgnization function, it's really simple.
I just wanna make sure to go the right way and the most optimized one

lofty light
#

For avoiding duplicates, you can choose to create a "Service" class that could perform your double used action. Or create two actions for two situations (create from data array, or from organization data)

faint forge
#

Thanks i'll look into that

faint forge
faint forge
lofty light
#

If it's a Facade, it means it's global

faint forge
#

Okok, big thanks Ill keep reading 😄