Skip to content

LessThanValue#

A validation rule that checks if the POSTed value is less than a value found in a database row.

Please note, this validation fails when it can not find a record to compare against.

Class#

LaravelExtendedValidation\Rules\Database\LessThanValue

Constructor argument(s)#

$table = '' // Table name to search in
$column = '' // Column the compare the value with.
$identifierColumn = '' // Identifying column
$uniqueIdentifier = '' // Unique identifying value for a row (Would most likely be a primary key value)

Example#

class DiscountPriceController extends Controller 
{
    public function storeDiscountedPrice(Request $request)
    {
        $validated = $request->validate([
            'product_id' => [
                'required',
                'numeric',
                'exists:products,id'
            ],
            'price' => [
                'required'
                'numeric' ,
                 new LessThanValue(
                    'products',
                    'price',
                    'id',
                    $request->product_id,
                );
            ],
        ]);

        return Discount::create([
            'product_id' => $validated->['product_id'],
            'price' => $validated->['price']
        ]);
    }

}