QueryException in laravel 6 Exception Handling

Today, We want to share with you QueryException in laravel 6 Exception Handling.In this post we will show you Simplify Laravel Error Logging with PHP Rollbar Integration, hear for Laravel Exceptions: How to Catch, Handle and Create Your Own we will give you demo and example for implement.In this post, we will learn about how to display exception handling error message in laravel with an example.

QueryException in laravel 6 Exception Handling

There are the Following The simple About laravel catch exception in controller Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel error handling best practices, so the laravel catch exception in controller is used for this example is following below.

Read Also:  Laravel Handling findOrFail exceptions

Database Transactions in Laravel 6

Create Team & Create Client

// Create Team
$freshMember = Team::create([
    'teamname' => Input::get('teamname'),
]);

// Create Client
$freshClient = Client::create([
    'client' => Input::get('client'),
    'account_id' => $freshMember->id,
]);

When Team was not created & Client was not created.

beginTransaction();

$acct = createTeam();
$user = createClient();


if( !$acct || !$user )
{
    rollbackTransaction();
} else {
    commitTransaction();
}

Basic Transactions in Laravel

using the DB::transaction() method:

DB::transaction(function()
{
    $freshMember = Team::create([
        'teamname' => Input::get('teamname')
    ]);

    $freshClient = Client::create([
        'client' => Input::get('client'),
        'account_id' => $freshMember->id,
    ]);
});

laravel error handling best practices

    public function transaction(Closure $callback)
    {
            $this->beginTransaction();

            try
            {
                    $result = $callback($this);

                    $this->commit();
            }

            catch (\Exception $e)
            {
                    $this->rollBack();

                    throw $e;
            }

            return $result;
    }

DB::transaction(function()
{
    $freshMember = Team::create([
        'teamname' => Input::get('teamname')
    ]);

    $freshClient = Client::create([
        'client' => Input::get('client'),
        'account_id' => $freshMember->id,
    ]);

    if( !$freshClient )
    {
        throw new \Exception('Client not created for account');
    }
});

Advanced Transactions in Laravel

try {
    $freshMember = Team::create( ['teamname' => Input::get('teamname')] );
} catch(ValidationException $e)
{

    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
}

try {
    $freshClient = Client::create([
        'client' => Input::get('client'),
        'account_id' => $freshMember->id
    ]);
} catch(ValidationException $e)
{
    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
}

DB::beginTransaction();

try {
    $freshMember = Team::create( ['teamname' => Input::get('teamname')] );
} catch(ValidationException $e)
{

    DB::rollback();
    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
} catch(\Exception $e)
{
    DB::rollback();
    throw $e;
}

try {
    $freshClient = Client::create([
        'client' => Input::get('client'),
        'account_id' => $freshMember->id
    ]);
} catch(ValidationException $e)
{

    DB::rollback();
    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
} catch(\Exception $e)
{
    DB::rollback();
    throw $e;
}
DB::commit();

Web Programming Tutorials Example with Demo

Read :

Read Also:  Convert object to array in Angularjs Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel throw exception with message.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.