Martney Acha
Martney Acha

Reputation: 3012

A non-numeric value encountered Laravel Query Builder

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.

Upvotes: 2

Views: 6573

Answers (3)

Mihir Bhende
Mihir Bhende

Reputation: 9045

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

Upvotes: 2

Supun Praneeth
Supun Praneeth

Reputation: 3160

In your code here: 'total_receivables' - $total_receivable and here: 'total_payment' - $total_receivable you are trying to subtract a number from a string, this is a php 7.1 error.

You have to get total_receivables and total_receivables separately and subtract then update the table.

Upvotes: 0

Sahil Darji
Sahil Darji

Reputation: 234

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

Upvotes: -1

Related Questions