laravel try catch exception; In this tutorial, you will learn about laravel try catch exception. 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.
You can use try catch as below syntax and you just need to use “use Exception” on top of the file in laravel.
Here is the syntax representing the try..catch statement in laravel:
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.