Laravel Division by Zero Exception - Solved
Hi Dev,
In this short tutorial, we will cover a division by zero exception laravel. you can understand a concept of division by zero exception laravel error. step by step explain division by zero error laravel. if you want to see an example of laravel divide by zero exception then you are in the right place.
Sometime we work with attempting to divide a number by zero will result in a "Division by zero" exception being thrown. You can handle this exception gracefully in your code to avoid crashes and provide a more user-friendly response. We need to resolve that it's call run time error. so we can solve by two ways. you can see the one by one solution with example.
Error Page:
Laravel Division by Zero Exception Solved using custom logic
you can see the controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$value = 10;
$denominator = 0;
$result = $denominator == 0 ? 0 : ($value / $denominator);
dd($result);
}
}
Laravel Division by Zero Exception Solved using DivisionByZeroError
you can see the controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DivisionByZeroError;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$value = 10;
$denominator = 0;
try {
$result = $value / $denominator;
} catch (DivisionByZeroError $e) {
$result = 0;
}
dd($result);
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Connect PostgreSQL Database in Laravel?
- How to use Sqlite Database in Laravel?
- How to Use Break And Continue In Laravel Blade Foreach?
- Laravel Blade Foreach Last Element Example
- How to Encrypt Decrypt Database Fields in Laravel?
- How to Generate Secure Https URL from Route in Laravel?
- Laravel Relationships with Optional() Helper Example
- How to Send Mail using PHPMailer in Laravel?
- Laravel Wordpress REST API Tutorial Example
- How to Page Break in PDF using DomPDF Laravel?
- Laravel 10 Generate PDF File using DomPDF Example