Posted inTechnology / Laravel / Mysql / Mysqli / php / Programming

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.

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 :

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype