【问题标题】:A non-numeric value encountered Laravel Query BuilderLaravel Query Builder 遇到的非数字值
【发布时间】:2019-02-18 03:40:18
【问题描述】:

我有一个工作功能,但现在突然提示错误。

at HandleExceptions->handleError(2, '遇到非数值', 'C:\xampp\htdocs\fdis-laravel\app\Receivable.php', 67, 数组('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。

【问题讨论】:

  • 你想在这里做什么'total_receivables' - $total_receivable

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


【解决方案1】:

您的 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;
}

【讨论】:

  • 谢谢你!!!有用 !!!我有一个空变量,这就是为什么!!!谢谢
【解决方案2】:

在您的代码中:'total_receivables' - $total_receivable 和此处:'total_payment' - $total_receivable 您试图从字符串中减去一个数字,这是 php 7.1 错误。

您必须分别获得total_receivablestotal_receivables 并减去然后更新表格。

【讨论】:

    【解决方案3】:

    您需要将其转换为可以定义的数字格式 像这样的函数参数数据类型

    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;
    }
    

    【讨论】:

    • 这和参数数据类型无关,是在字符串中减去一个数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多