Laravel查询生成器遇到非数值

时间:2019-02-18 03:40:18

标签: php laravel laravel-5.4 laravel-query-builder

我有一个正在运行的功能,但突然它现在提示错误。

  

在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。

3 个答案:

答案 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_receivabletotal_receivables,然后减去然后更新表格。

答案 2 :(得分:-1)

  

您需要将其转换为数字格式才能定义   像这样的功能参数数据类型

const isInvalidInput = val => parseFloat(val) < 0.5 || isNaN(val);