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 6 Create Custom Exception Error Page Example

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:  vue Select Box binding 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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Handling findOrFail exceptions
  2. Read Also:  Angular 9/8 SEO Dynamic Rendering Title and Meta Tags
  3. Read Also:  Ajax Upload Multiple Images without form submitting
  4. Read Also:  PHP Laravel Autocomplete search from Database Example
  5. Read Also:  int to string c++
  6. Read Also:  Laravel Exception Handling Example Tutorial
  7. Read Also:  Laravel Form Automatic model validation Example
  8. Read Also:  PHP Laravel 6 Custom Error Page Example
  9. Read Also:  Group By with Multiple Columns using Laravel Collection
  10. Read Also:  Count Query Codeigniter Tutorial