Skip to content

Commit

Permalink
Merge pull request #28 from YunusEmreNalbant/main
Browse files Browse the repository at this point in the history
Laravel 11 and PHP 8.3 support
  • Loading branch information
YunusEmreNalbant authored Apr 9, 2024
2 parents a68b379 + 8f59fae commit fe243d6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 74 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ['8.0', 8.1, 8.2]
laravel: [8, 9, 10]
php: ['8.0', 8.1, 8.2, 8.3]
laravel: [8, 9, 10, 11]
exclude:
- php: '8.0'
laravel: 10
- php: '8.0'
laravel: 11
- php: '8.2'
laravel: 8
- php: '8.1'
laravel: 11

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

Expand Down
72 changes: 60 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ This is a Laravel package to work with geospatial data types and functions.

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

### Laravel Compatibility

| Version | Supported Laravel Versions |
|---------|----------------------------|
| `2.x` | `^11.0` |
| `1.x` | `^8.0, ^9.0, ^10.0` |

**Supported data types:**
- `Point`

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

### 1- Migrations:

## Code Differences Based on Laravel Version

Some code snippets in the project differ before and after Laravel 11 version. Below are the steps to specify these differences:

### For Laravel 8, 9, and 10 Versions

To add a spatial data field, you need to extend the migration from `TarfinLabs\LaravelSpatial\Migrations\SpatialMigration`.

It is a simple abstract class that adds `point` spatial data type to Doctrine mapped types in the constructor.

```php
use TarfinLabs\LaravelSpatial\Migrations\SpatialMigration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends SpatialMigration {

public function up(): void
{
Schema::create('addresses', function (Blueprint $table) {
$table->point('location');
})
}

}
```

Expand All @@ -64,16 +75,38 @@ So you should give an SRID attribute to use spatial indexes in the migrations an
```php
Schema::create('addresses', function (Blueprint $table) {
$table->point(column: 'location', srid: 4326);

$table->spatialIndex('location');
})
```
### For Laravel 11 and Above Versions

From Laravel 11 onwards, migrations are created as follows:

```php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {

public function up(): void
{
Schema::create('addresses', function (Blueprint $table) {
$table->geography('location', 'point');
})
}

}
```
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.

#### Issue with adding a new location column with index to an existing table:
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.

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

### For Laravel 8, 9, and 10 Versions
```php
public function up()
{
Expand All @@ -93,6 +126,28 @@ public function up()
}
```


### For Laravel 11 and Above Versions

```php
public function up()
{
// Add the new location column as nullable
Schema::table('table', function (Blueprint $table) {
$table->geography('location', 'point')->nullable();
});

// In the second go, set 0,0 values, make the column not null and finally add the spatial index
Schema::table('table', function (Blueprint $table) {
DB::statement("UPDATE `addresses` SET `location` = ST_GeomFromText('POINT(0 0)', 4326);");

DB::statement("ALTER TABLE `table` CHANGE `location` `location` POINT NOT NULL;");

$table->spatialIndex('location');
});
}
```

***

### 2- Models:
Expand Down Expand Up @@ -298,14 +353,6 @@ Either way, you will get the following output for the location casted field:
composer test
```

### Road Map
- [ ] MultiPoint
- [ ] LineString
- [ ] MultiLineString
- [ ] Polygon
- [ ] MultiPolygon
- [ ] GeometryCollection

### Changelog

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

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

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
}
],
"require": {
"php": "^8.0|^8.1|^8.2",
"illuminate/support": "^8.0|^9.0|^10.0"
"php": "^8.0|^8.1|^8.2|^8.3",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"orchestra/testbench": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
Expand Down
23 changes: 0 additions & 23 deletions src/Doctrine/Point.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/LaravelSpatialServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace TarfinLabs\LaravelSpatial;

use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use TarfinLabs\LaravelSpatial\Doctrine\Point;

class LaravelSpatialServiceProvider extends ServiceProvider
{
Expand All @@ -28,19 +25,5 @@ public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-spatial');

if (class_exists(Type::class)) {
// Prevent geometry type fields from throwing a 'type not found' error when changing them
$geometries = [
'point' => Point::class,
];
$typeNames = array_keys(Type::getTypesMap());
foreach ($geometries as $type => $class) {
if (!in_array($type, $typeNames)) {
Type::addType($type, $class);
DB::connection()->registerDoctrineType($class, $type, $type);
}
}
}
}
}
14 changes: 0 additions & 14 deletions src/Migrations/SpatialMigration.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Types/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Point

protected string $wktOptions;

public function __construct(float $lat = 0, float $lng = 0, ?int $srid = null)
public function __construct(float $lat = 0, float $lng = 0, ?int $srid = 4326)
{
$this->lat = $lat;
$this->lng = $lng;
Expand Down
2 changes: 1 addition & 1 deletion tests/LocationCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function it_can_set_the_casted_attribute_to_a_point(): void
$response = $cast->set($address, 'location', $point, $address->getAttributes());

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

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_in_the
// 2. Assert
$this->assertSame(expected: 0.0, actual: $point->getLat());
$this->assertSame(expected: 0.0, actual: $point->getLng());
$this->assertSame(expected: 0, actual: $point->getSrid());
$this->assertSame(expected: 4326, actual: $point->getSrid());
}

/** @test */
Expand Down

0 comments on commit fe243d6

Please sign in to comment.