How to use the ROUND function in Laravel

Asked

Viewed 67 times

-5

insira o código aquiI have the following query in PHP:

$transferencia = $conecta->rawSelect(
    'SELECT * FROM register WHERE ROUND(valor,2)="' . $json->VALORLIQUIDO . '"  '
);
                   

How I perform this same consultation using the Eloquent Laravel?

This value field is a value float.

tried so

$transferencia = Register::where('valor',$json->VALORLIQUIDO)->get()

It does not find any results, even if it is certain that it has this record in the database, as the column has a value float the query does not locate the result. So I need to understand how to perform a query in the Laravel using eloquent to search for records in columns that have float values.

1 answer

-2


Assuming you have the Register model created correctly.

$transferencia = Register::where('ROUND(valor,2)',$json->VALORLIQUIDO)->get();
$transferencia = Register::whereRaw('ROUND(valor,2)',$json->VALORLIQUIDO)->get();

DB::table('register')->whereRaw('ROUND(valor,2)', $json->VALORLIQUIDO)->get();
DB::table('register')->whereRaw('ROUND(valor,2)="' . $json->VALORLIQUIDO . '"')->get();

From one tested on those and get feedback

  • 1

    Matthew, in the case of this query above I don’t need to indicate which column ?

  • It was barely written fast and I forgot

  • Matthew Tried like this $value = 1.11; $Register = Register::Where('value', $value)->Where(round($value, 2), $value)->get(); message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '1.11' in 'Wclause' (SQL: select * from here register Where valor = 1.11 and 1.11 = 1.11)"

  • Now that I read it calmly there, had seen it wrong, then in this case you will have to use DB::raw

  • Just having a while I ride there

  • Worked 100% Matthew! excellent solution.

  • @Murilo which one did you use?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.