Laravel/Eloquent Model query 'orderBy' does not seem to always work when the table contains a mysql geometry column #34796
Unanswered
TomasJohansson
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version: 8.9.0
PHP Version: 7.4.3
Database Driver & Version:
I can also mention that I am using mysql server 8.0.21 at my Linux Ubuntu 20.04 machine.
Description:
I have imported a shapefile (with 21 polygons) to a mysql table by using the tool ogr2ogr (version GDAL 3.0.4), but when trying to retrieve rows by using the Laravel/Eloquent model method 'orderBy' it does not work when sorting by a varchar field unless I remove most of the rows, though the method 'orderBy' does indeed work when sorting by the auto increment column.
Steps To Reproduce:
The shapefile (with counties of Sweden) I used which you also can use for reproducing the problem:
Download (2 MB) the zip file https://data.val.se/val/val2018/statistik/2018_valgeografi_lan.zip
(linked from this page https://data.val.se/val/val2018/statistik/index.html )
Unzip to a directory which will include some "alla_lan.*" files, e.g. the shapefile "alla_lan.shp".
(BTW, "alla län" is swedish for "all counties")
Create a mysql database (the name "YOUR_DATABASE" is used below) with enough permissions for "YOUR_USERNAME" to create a database table there with the below command.
ogr2ogr -f MySQL MySQL:YOUR_DATABASE,host=127.0.0.1,user=YOUR_USERNAME,password=YOUR_PASSWORD /YOUR_PATH_TO_THE_UNZIPPED_SHAPEFILE/alla_lan.shp -lco ENGINE=InnoDB -t_srs EPSG:4326
The above command should now have created a table "alla_lan" in "YOUR_DATABASE" with 21 rows.
The column SHAPE is a "geometry" (mysql type) column defining a geographic area (county) of Sweden (i.e. there are 21 counties in Sweden).
Create an Eloquent Model as below in a file:
Now try to use the "AllaLan" class as below from some test file (which also should include "use App\Models\AllaLan;") :
The first row above is correct i.e. there are 21 rows in the table, but when sorting by another column the result is zero !
As mentioned there are 21 rows in the table, but if I remove many of them it works (to sort by "lan_namn") i.e. then the correct number is returned instead of zero.
For example, if I delete all but 5 rows like this with some mysql client:
delete from alla_lan where OGR_FID > 5;
Then it still does not work, but if I remove one more i.e. if I run this:
delete from alla_lan where OGR_FID > 4;
then it does work by correctly returning the now remaining 4 rows instead of returning zero.
print("Rows returned when sorting by lan_namn: " . count( AllaLan::orderBy("lan_namn", "asc")->get() ) ); # 4
Beta Was this translation helpful? Give feedback.
All reactions