Skip to content
Advertisement

Model not returning value when one field used in select

I have a question with my if statement. I want to get check what is bigger than another price value. one value is saved in my database table and another one is getting with input value. but I wrote some if statement it isn’t working.

this is my table stored price column structure.

rest_payment -> double(11, 2)

and I got another value using request

$payment = $request->input('payment');

my controller function like this

$checkRestPayment = DB::table('sales')
                    ->select('rest_payment')
                    ->where('bill_no', $request->input('bill_no'))
                    ->first();

$payment = $request->input('payment');   

if($checkRestPayment < $payment)
{
  return back()->with('error', 'Your payment value exceed rest payment value in this bill no!');
}

can anyone help me to fix this error?

Advertisement

Answer

Your $checkRestPayment is a Model object. In order to compare the field, you need to reference the rest_payment attribute:

if($checkRestPayment->rest_payment < $payment)

What you could also do is let the database do the comparison for you:

// this is now a boolean
$hasRestPayment = DB::table('sales')
                  ->where('bill_no', $request->input('bill_no'))
                  ->where('rest_payment', '<', $request->input('payment'))
                  ->exists();

if($hasRestPayment)
{
  return back()->with('error', 'Your payment value exceed rest payment value in this bill no!');
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement