skip to Main Content

This working with mysql (5.7.32) php (8.0.0) and laravel 8.
I get lat and lng using leaflet. in fact I did tests saving the number manually in the database and the same error occurs

when I look at the database with phpmyadmin I see exactly the number it should be:
-32.84240531063937 but when I bring those values ​​with laravel and do dd() -32.842405310639
remove the last numbers (37).

the data type on mysql is DOUBLE without Length/Values

this is how i get the numbers from the database.

I have a table called complaints where each of them has lat and lng
So now I need to get all the records that have (comune_name = Llay Lay)
and it is from there where I get lat and lng from the first record.


$complaints = Complaint::where('comune_name', 'Llay Llay')->get()->toArray();

$denounced_lat = $complaints[0]['lat']; //lat of first complaint
$denounced_lng = $complaints[0]['lng']; //lng of first complaint

  dd($denounced_lat); //acá está el error

The number I see in the database is: -32.84240531063937
and the number that dd () draws is: -32.842405310639

thnks 🙂

EDIT: Finally i save as VARCHAR.

2

Answers


  1. PHP works with different precision compared to your database solution.

    Consider storing them as 64-bit signed ints if you want to keep the precision.

    Login or Signup to reply.
  2. to hold a long number after the comma ‘,’ you can use decimal:

        $table->decimal('lng', 10, 18);
        $table->decimal('lat', 10, 18);
    

    The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:

    M is the maximum number of digits (the precision). It has a range of 1 to 65.

    D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

    it worth to say that you should cast it to float in $cast attribute in your model, in Laravel doc, there is a decimal cast but it’s not ready yet!
    for more info see this answer.

    see mysql decimal

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search