In this tutorial you will learn about the How to Use try catch In laravel Example Tutorial and its application with practical example.
In this How to Use try catch In laravel Example TutorialI will show you How to Use try catch In laravel.In this tutorial you will learn to use try catch exception handling in laravel framework. While working or developing application with Laravel framework, you may encounter many errors. It is very important to handle these errors properly in order to build safe and error free laravel application. In laravel you can use try catch statement for proper error handling in laravel.
How to Use try catch In laravel Example Tutorial
Below is the syntax that represents the try..catch statement:
1 2 3 4 5 6 |
try { // run your code here } catch (exception $e) { //code to handle the exception } |
The try…catch statement is used to handle the errors.
Find Product By Title
Let’s take a look example of laravel try catch. Here you have product table and find the product with it’s title. So you have the following things:
routes/web.php
1 2 |
Route::get('/product', 'ProductController@index')->name('product.index'); Route::post('/product/search', 'ProductController@search')->name('product.search'); |
Controller with two methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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')); } } |
Blade view files as following:
index.blade.php
1 2 3 4 5 6 7 |
<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
1 2 3 4 |
<h3 class="page-title text-center">Product found: {{ $product->title }}</h3> <b>Price</b>: {{ $product->price }} <br> <b>Code</b>: {{ $product->code }} |
Get Errors
In the above example if the product is found, there is no error but if a product is not found, we will encounter some errors on your search.blade.php file.
Now, go to .env file and set
1 |
APP_DEBUG=false |
Then the browser will just show blank screen, looks like something went wrong. But that still doesn’t give any valuable information to our visitor. If the product is not found there will error occurs, so you can pass errors on your search.blade.php file using try..catch statement as following:
1 2 3 4 5 6 7 8 9 |
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')); } |
we can display an error message in Blade file as following
1 2 3 4 5 6 7 |
<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> |