Home > Software design >  How to only add the positive numbers in eloquent | Laravel 9
How to only add the positive numbers in eloquent | Laravel 9

Time:08-13

I'm trying to add integer records from my DB. they are multiple records in the 'total' column. However, there are also some negative values. How can I distinguish them from the positive values? I want to only add the positive numbers; when I use the sum() method it gives me the wrong result

This is what I tried:

$savings = DB::table('savings')->sum('total');

CodePudding user response:

You'd use where:

DB::table('savings')->where('total', '>', 0)->sum('total');

CodePudding user response:

You can use validation in laravel

$this->validate(
 "total" => 'min:1",
);

or use

Form::number('quantity', null, ['class' => 'form-control', 'min' => 1])

in html also you can use min attribute

  • Related