I have a working function, but suddenly it now prompts an error.
at HandleExceptions->handleError(2, 'A non-numeric value encountered', 'C:\xampp\htdocs\fdis-laravel\app\Receivable.php', 67, array('receivable_payment_head_id' => null, 'total_receivable' => '936.341')) in Receivable.php line 67
Here's my code using DB:Raw.
<?php
public static function updateLessPaymentHead($receivable_payment_head_id, $total_receivable)
{
$payment_head = DB::table('receivables_payment_head')
->where('id', $receivable_payment_head_id)
->update(
['total_receivables' => DB::raw('total_receivables' - $total_receivable),
'total_payment' => DB::raw('total_payment' - $total_receivable),
]);
return $payment_head;
}
Is there a way that I can address the non-numeric issue with DB:raw or do I need to first convert this to numeric before updating? I'm using Laravel 5.4 and PHP 7.1.
There is an error in your DB::raw.
DB::raw('total_receivables' - $total_receivable)
will essentially try to subtract the value of $total_receivable
from string total_receivables
. However, I believe you need to subtract it from the value of column total_receivable
. Then you need to change it to :
DB::raw('total_receivables - ' . $total_receivable )
Please check updated code :
<?php
public static function updateLessPaymentHead($receivable_payment_head_id, $total_receivable)
{
if(!is_numeric($receivable_payment_head_id) || !is_numeric($total_receivable)){
return [];
}
$payment_head = DB::table('receivables_payment_head')
->where('id', $receivable_payment_head_id)
->update(
[
'total_receivables' => DB::raw('total_receivables - ' . $total_receivable ),
'total_payment' => DB::raw('total_payment - ' . $total_receivable),
]);
return $payment_head;
}