Skip to content

Commit fe243d6

Browse files
Merge pull request #28 from YunusEmreNalbant/main
Laravel 11 and PHP 8.3 support
2 parents a68b379 + 8f59fae commit fe243d6

File tree

9 files changed

+72
-74
lines changed

9 files changed

+72
-74
lines changed

.github/workflows/main.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: ['8.0', 8.1, 8.2]
12-
laravel: [8, 9, 10]
11+
php: ['8.0', 8.1, 8.2, 8.3]
12+
laravel: [8, 9, 10, 11]
1313
exclude:
1414
- php: '8.0'
1515
laravel: 10
16+
- php: '8.0'
17+
laravel: 11
1618
- php: '8.2'
1719
laravel: 8
20+
- php: '8.1'
21+
laravel: 11
1822

1923
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
2024

README.md

+60-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ This is a Laravel package to work with geospatial data types and functions.
88

99
It supports only MySQL Spatial Data Types and Functions, other RDBMS is on the roadmap.
1010

11+
### Laravel Compatibility
12+
13+
| Version | Supported Laravel Versions |
14+
|---------|----------------------------|
15+
| `2.x` | `^11.0` |
16+
| `1.x` | `^8.0, ^9.0, ^10.0` |
17+
1118
**Supported data types:**
1219
- `Point`
1320

@@ -34,24 +41,28 @@ php artisan make:model Address --migration
3441

3542
### 1- Migrations:
3643

44+
## Code Differences Based on Laravel Version
45+
46+
Some code snippets in the project differ before and after Laravel 11 version. Below are the steps to specify these differences:
47+
48+
### For Laravel 8, 9, and 10 Versions
49+
3750
To add a spatial data field, you need to extend the migration from `TarfinLabs\LaravelSpatial\Migrations\SpatialMigration`.
3851

3952
It is a simple abstract class that adds `point` spatial data type to Doctrine mapped types in the constructor.
40-
4153
```php
4254
use TarfinLabs\LaravelSpatial\Migrations\SpatialMigration;
4355
use Illuminate\Database\Schema\Blueprint;
4456
use Illuminate\Support\Facades\Schema;
4557

4658
return new class extends SpatialMigration {
47-
59+
4860
public function up(): void
4961
{
5062
Schema::create('addresses', function (Blueprint $table) {
5163
$table->point('location');
5264
})
5365
}
54-
5566
}
5667
```
5768

@@ -64,16 +75,38 @@ So you should give an SRID attribute to use spatial indexes in the migrations an
6475
```php
6576
Schema::create('addresses', function (Blueprint $table) {
6677
$table->point(column: 'location', srid: 4326);
67-
78+
6879
$table->spatialIndex('location');
6980
})
7081
```
82+
### For Laravel 11 and Above Versions
83+
84+
From Laravel 11 onwards, migrations are created as follows:
85+
86+
```php
87+
use Illuminate\Database\Migrations\Migration;
88+
use Illuminate\Database\Schema\Blueprint;
89+
use Illuminate\Support\Facades\Schema;
90+
91+
return new class extends Migration {
92+
93+
public function up(): void
94+
{
95+
Schema::create('addresses', function (Blueprint $table) {
96+
$table->geography('location', 'point');
97+
})
98+
}
99+
100+
}
101+
```
102+
In Laravel 11, the methods **point**, **lineString**, **polygon**, **geometryCollection**, **multiPoint**, **multiLineString**, and **multiPolygon** have been removed. Therefore, we are updating to use the **geography** method instead. The `geography` method sets the default SRID value to 4326.
71103

72104
#### Issue with adding a new location column with index to an existing table:
73105
When adding a new location column with an index in Laravel, it can be troublesome if you have existing data. One common mistake is trying to set a default value for the new column using `->default(new Point(0, 0, 4326))`. However, `POINT` columns cannot have a default value, which can cause issues when trying to add an index to the column, as indexed columns cannot be nullable.
74106

75107
To solve this problem, it is recommended to perform a two-step migration like following:
76108

109+
### For Laravel 8, 9, and 10 Versions
77110
```php
78111
public function up()
79112
{
@@ -93,6 +126,28 @@ public function up()
93126
}
94127
```
95128

129+
130+
### For Laravel 11 and Above Versions
131+
132+
```php
133+
public function up()
134+
{
135+
// Add the new location column as nullable
136+
Schema::table('table', function (Blueprint $table) {
137+
$table->geography('location', 'point')->nullable();
138+
});
139+
140+
// In the second go, set 0,0 values, make the column not null and finally add the spatial index
141+
Schema::table('table', function (Blueprint $table) {
142+
DB::statement("UPDATE `addresses` SET `location` = ST_GeomFromText('POINT(0 0)', 4326);");
143+
144+
DB::statement("ALTER TABLE `table` CHANGE `location` `location` POINT NOT NULL;");
145+
146+
$table->spatialIndex('location');
147+
});
148+
}
149+
```
150+
96151
***
97152

98153
### 2- Models:
@@ -298,14 +353,6 @@ Either way, you will get the following output for the location casted field:
298353
composer test
299354
```
300355

301-
### Road Map
302-
- [ ] MultiPoint
303-
- [ ] LineString
304-
- [ ] MultiLineString
305-
- [ ] Polygon
306-
- [ ] MultiPolygon
307-
- [ ] GeometryCollection
308-
309356
### Changelog
310357

311358
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
@@ -326,3 +373,4 @@ If you discover any security related issues, please email [email protected]
326373
## License
327374

328375
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
376+

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.0|^8.1|^8.2",
20-
"illuminate/support": "^8.0|^9.0|^10.0"
19+
"php": "^8.0|^8.1|^8.2|^8.3",
20+
"illuminate/support": "^8.0|^9.0|^10.0|^11.0"
2121
},
2222
"require-dev": {
2323
"doctrine/dbal": "^3.3",
24-
"orchestra/testbench": "^6.0|^7.0|^8.0",
24+
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
2525
"phpunit/phpunit": "^9.0"
2626
},
2727
"autoload": {

src/Doctrine/Point.php

-23
This file was deleted.

src/LaravelSpatialServiceProvider.php

-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace TarfinLabs\LaravelSpatial;
44

5-
use Doctrine\DBAL\Types\Type;
6-
use Illuminate\Support\Facades\DB;
75
use Illuminate\Support\ServiceProvider;
8-
use TarfinLabs\LaravelSpatial\Doctrine\Point;
96

107
class LaravelSpatialServiceProvider extends ServiceProvider
118
{
@@ -28,19 +25,5 @@ public function register()
2825
{
2926
// Automatically apply the package configuration
3027
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-spatial');
31-
32-
if (class_exists(Type::class)) {
33-
// Prevent geometry type fields from throwing a 'type not found' error when changing them
34-
$geometries = [
35-
'point' => Point::class,
36-
];
37-
$typeNames = array_keys(Type::getTypesMap());
38-
foreach ($geometries as $type => $class) {
39-
if (!in_array($type, $typeNames)) {
40-
Type::addType($type, $class);
41-
DB::connection()->registerDoctrineType($class, $type, $type);
42-
}
43-
}
44-
}
4528
}
4629
}

src/Migrations/SpatialMigration.php

-14
This file was deleted.

src/Types/Point.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Point
1414

1515
protected string $wktOptions;
1616

17-
public function __construct(float $lat = 0, float $lng = 0, ?int $srid = null)
17+
public function __construct(float $lat = 0, float $lng = 0, ?int $srid = 4326)
1818
{
1919
$this->lat = $lat;
2020
$this->lng = $lng;

tests/LocationCastTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function it_can_set_the_casted_attribute_to_a_point(): void
3636
$response = $cast->set($address, 'location', $point, $address->getAttributes());
3737

3838
// 3. Assert
39-
$this->assertEquals(DB::raw("ST_GeomFromText('{$point->toWkt()}')"), $response);
39+
$this->assertEquals(DB::raw("ST_GeomFromText('{$point->toWkt()}', 4326, 'axis-order=long-lat')"), $response);
4040
}
4141

4242
/** @test */

tests/PointTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_in_the
3535
// 2. Assert
3636
$this->assertSame(expected: 0.0, actual: $point->getLat());
3737
$this->assertSame(expected: 0.0, actual: $point->getLng());
38-
$this->assertSame(expected: 0, actual: $point->getSrid());
38+
$this->assertSame(expected: 4326, actual: $point->getSrid());
3939
}
4040

4141
/** @test */

0 commit comments

Comments
 (0)