vendredi 5 juin 2020

how can i avoid error Division by zero laravel?

this is my function

$total = Reservation::count();

$newreservations = Reservation::where('status','=','Pending')->count();
$newreservationsperc = $newreservations / $total * 100;

$acceptedreservations = Reservation::where('status','=','Accepté')->count();
$acceptedreservationsperc = $acceptedreservations / $total * 100;

$Refusedreservations = Reservation::where('status','=','Refusé')->count();
$Refusedreservationsperc = $Refusedreservations / $total * 100;
return view('admin.dashboard',compact('hotels','reservations','clients','messages','newreservationsperc','acceptedreservationsperc','Refusedreservationsperc','newreservations','acceptedreservations','Refusedreservations'));

when the $total is not 0 its working fine but when its null i got this error Division by zero

i try this

$total = Reservation::count();

if ($total != 0) {
    $newreservations = Reservation::where('status','=','Pending')->count();
    $newreservationsperc = $newreservations / $total * 100;

    $acceptedreservations = Reservation::where('status','=','Accepté')->count();
    $acceptedreservationsperc = $acceptedreservations / $total * 100;

    $Refusedreservations = Reservation::where('status','=','Refusé')->count();
    $Refusedreservationsperc = $Refusedreservations / $total * 100;
    return view('admin.dashboard',compact('hotels','reservations','clients','messages','newreservationsperc','acceptedreservationsperc','Refusedreservationsperc','newreservations','acceptedreservations','Refusedreservations'));

} else {
    return  view('admin.dashboard');
}

but i got others error on my chart



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire