遇到非数字值 Laravel 查询生成器
A non-numeric value encountered Laravel Query Builder
我有一个可以工作的功能,但是现在突然提示错误。
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
这是我使用 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.
You need to convert that in numeric format for that you can define
function parameter datatype like this
public static function updateLessPaymentHead(int $receivable_payment_head_id, int $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;
}
在您的代码中:'total_receivables' - $total_receivable
和此处:'total_payment' - $total_receivable
您正试图从字符串中减去一个数字,这是一个 php 7.1 错误。
您必须分别获取 total_receivables
和 total_receivables
并相减然后更新 table。
您的 DB::raw 有误。
DB::raw('total_receivables' - $total_receivable)
本质上会尝试从字符串 total_receivables
中减去 $total_receivable
的值。但是,我相信您需要从 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;
}
我有一个可以工作的功能,但是现在突然提示错误。
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
这是我使用 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.
You need to convert that in numeric format for that you can define function parameter datatype like this
public static function updateLessPaymentHead(int $receivable_payment_head_id, int $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;
}
在您的代码中:'total_receivables' - $total_receivable
和此处:'total_payment' - $total_receivable
您正试图从字符串中减去一个数字,这是一个 php 7.1 错误。
您必须分别获取 total_receivables
和 total_receivables
并相减然后更新 table。
您的 DB::raw 有误。
DB::raw('total_receivables' - $total_receivable)
本质上会尝试从字符串 total_receivables
中减去 $total_receivable
的值。但是,我相信您需要从 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;
}