我有一个正在运行的功能,但突然它现在提示错误。
在HandleExceptions-> handleError(2,'遇到一个非数字值', 'C:\ xampp \ htdocs \ fdis-laravel \ app \ Receivable.php',67, array('receivable_payment_head_id'=> null,'total_receivable'=> '936.341'))在Receivable.php 67行中
这是我使用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;
}
有没有一种方法可以解决DB:raw的非数字问题,还是在更新之前需要先将其转换为数字?我正在使用Laravel 5.4和PHP 7.1。
答案 0 :(得分:1)
您的DB :: raw中存在错误。
DB::raw('total_receivables' - $total_receivable)
实际上将尝试从字符串$total_receivable
中减去total_receivables
的值。但是,我相信您需要从total_receivable
列的值中对其进行子跟踪。然后,您需要将其更改为:
DB::raw('total_receivables - ' . $total_receivable )
请检查更新的代码:
<?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;
}
答案 1 :(得分:0)
在您的代码中:storage.objects.create
和此处:'total_receivables' - $total_receivable
,您尝试从字符串中减去数字,这是php 7.1错误。
您必须分别获取'total_payment' - $total_receivable
和total_receivables
,然后减去然后更新表格。
答案 2 :(得分:-1)
您需要将其转换为数字格式才能定义 像这样的功能参数数据类型
const isInvalidInput = val => parseFloat(val) < 0.5 || isNaN(val);