Laravel try Catch
Laravel 14-Dec-2020

Laravel try Catch

In this tutorial, you will learn about laravel try catch.

If you are working with the Laravel framework, you may face many errors while working. To handle these errors, you can use try…catch statement.

Let’s assume that you are building a commerce web application in Laravel. And you have created a form in this web application that looks after searching the product.

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.

So you have the following things:

1: routes/web.php

Route::get('/product', 'ProductController@index')->name('product.index');
Route::post('/product/search', 'ProductController@search')->name('product.search');

2: In controller with two methods:

class ProductController extends Controller
{
    public function index()
    {
        return view('product.index');
    }
    public function search(Request $request)
    {
        $product = Product::where('title',$request->get('title'))->first();
        return view('product.search', compact('product'));
    }
}

3: Two blade view files,

index.blade.php

This file will display product form:

<form action="{{ route('product.search') }}" method="POST">
@csrf
<div class="form-group">
<input id="title" class="form-control" name="title" type="text" value="{{ old('product') }}" placeholder="Product Title">
</div>
<input class="btn btn-info" type="submit" value="Search">
</form>

search.blade.php

This file will display search result:

<h3 class="page-title text-center">Product found: {{ $product->title }}</h3>
<b>Price</b>: {{ $product->price }}
<br>
<b>Code</b>: {{ $product->code }}

Get Errors

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 web application.