How to handle failed CRUD operation

Posted 1 year ago by itstrueimryan

So I have a controller, OrganizationController, with the following create method:


return redirect('/organizations')->with('success', 'Organization successfully created and administrator invited via email!');

and its counterpart OrganizationRepository create method:

DB::transaction(function() use ($organization) {
        'name' => $data['name'],
        'slug' => slugify($data['name']),
        'account_type_id' => $data['account_type_id'],

        'username' => $data['admin_username'],
        'email' => $data['admin_email'],
        'password' => bcrypt('secret'),
        'verified' => 1,
        'organization_id' => $organization->id

My question is, what if the transaction fails? I want to show a nice flash message to the user informing them that there was a problem, but what is the best way to go about this? Should I wrap the transaction in a try/catch block, and have the redirect in the catch block with an error flash message? I feel like redirecting is more a controller thing then a repo thing...but it would be cool if I could just say $organization->create in the controller and it would only continue if the method succeeds, similar to request validation.

Please sign in or create an account to participate in this conversation.