Laravel 8 Try Catch in Controller Tutorial Example

Laravel 8 Try Catch in Controller Tutorial Example

Laravel 8 try catch statement example. In this tutorial, we will show you how to use try catch statements with laravel 8 in controller for error handing with exceptions.

If you are working with the Laravel version like 5, 6, 7, 8, you may face some errors while working. So, handle these errors, you can use try…catch statement in laravel 8 app.

Let’s assume that if you are building a commerce, social media, news and any web application in Laravel 8 app. And you have created some modules at that time, you may face any error or execption. So, you can use laravel 8 try catch statement for find this error in laravel 8 app.

But sometimes a user searches such a product. Which is not in your database. And you have not imposed any condition on the controller not getting the product. So at this time the user is not see anything on the search page or see something wrong in the error.

So in this situation, you can find the error by using Try-Catch and show the correct information to the user.

The syntax represents the try..catch statement:

try {
    // run your code here
}
catch (exception $e) {
    //code to handle the exception
}

The try…catch statement is used to handle the errors.

Let’s take a look example of laravel try catch:

Find Product By Title

Let’s take look a very easy example, Here you have product table and find the product with it’s title.

You can handle errors using try…catch statement in laravel. See the following representation of try..catch.

If you are not checking for product exist or not in database, And you can directory pass data to your blade file.

So there has two case, first one is if the product is found, there were no errors, but if a product is not found, there will be display some errors on your search.blade.php file.

Go to .env file and set APP_DEBUG=false and then the browser will just show blank Whoops, looks like something went wrong. But that still doesn’t give any valuable information to our visitor.

If the product is not found and error occurs, so you can pass errors on your search.blade.php file with try..catch statement.

See the following example:

public function search(Request $request)
{
    try {
        $product = Product::where('title',$request->get('title'));
    } catch (ModelNotFoundException $exception) {
        return back()->withError($exception->getMessage())->withInput();
    }
    return view('product.search', compact('product'));
}

If you want to display an error in Blade file, you can do look like:

<h3 class="page-title text-center">Search by proudct title</h3>

@if (session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif

<form action="{{ route('product.search') }}" method="POST">...</form>

Conclusion

In this laravel tutorial, you have learned how to use try catch statment in laravel 8 application.

Recommended Laravel Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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