Skip to content

Commit b6b6ee6

Browse files
authored
Merge pull request #67 from zigzagdev/feature/eloquent-fix
Create new migration files & Eloquent
2 parents d21cab4 + c367d3c commit b6b6ee6

5 files changed

Lines changed: 128 additions & 33 deletions

File tree

docker-compose.yml

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,46 @@ services:
1919
mysql:
2020
image: mysql:8.0
2121
container_name: heritage-mysql
22-
restart: unless-stopped
23-
ports:
24-
- "2306:3306"
2522
environment:
26-
MYSQL_DATABASE: world-heritage
23+
MYSQL_DATABASE: world_heritage_local
2724
MYSQL_ROOT_PASSWORD: world-heritage
2825
MYSQL_USER: world-heritage
2926
MYSQL_PASSWORD: world-heritage
30-
volumes:
31-
- db_data:/var/lib/mysql
27+
ports: ["2306:3306"]
28+
volumes: [db_data:/var/lib/mysql]
3229

3330
mysql-test:
3431
image: mysql:8.0
3532
container_name: heritage-mysql-test
36-
restart: unless-stopped
37-
ports:
38-
- "2307:3306"
3933
environment:
4034
MYSQL_DATABASE: world_heritage_test
4135
MYSQL_ROOT_PASSWORD: world-heritage
4236
MYSQL_USER: world-heritage
4337
MYSQL_PASSWORD: world-heritage
44-
volumes:
45-
- db_data_test:/var/lib/mysql
38+
ports: ["2307:3306"]
39+
volumes: [db_data_test:/var/lib/mysql]
4640

4741
phpmyadmin:
4842
image: phpmyadmin/phpmyadmin
4943
container_name: heritage-phpmyadmin
50-
restart: unless-stopped
51-
ports:
52-
- "8080:80"
5344
environment:
5445
PMA_HOST: mysql
5546
PMA_PORT: 3306
5647
PMA_USER: world-heritage
5748
PMA_PASSWORD: world-heritage
58-
depends_on:
59-
- mysql
49+
ports: ["8080:80"]
50+
depends_on: [mysql]
6051

6152
phpmyadmin-test:
6253
image: phpmyadmin/phpmyadmin
6354
container_name: heritage-phpmyadmin-test
64-
restart: unless-stopped
65-
ports:
66-
- "8081:80"
6755
environment:
6856
PMA_HOST: mysql-test
6957
PMA_PORT: 3306
7058
PMA_USER: world-heritage
7159
PMA_PASSWORD: world-heritage
72-
depends_on:
73-
- mysql-test
74-
75-
nginx:
76-
image: nginx:alpine
77-
container_name: heritage-nginx
78-
ports:
79-
- "1081:80"
80-
volumes:
81-
- ./src:/var/www
82-
- ./environment/nginx/default.conf:/etc/nginx/conf.d/default.conf
83-
depends_on:
84-
- app
60+
ports: ["8081:80"]
61+
depends_on: [mysql-test]
8562

8663
volumes:
8764
db_data:

src/app/Models/Country.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Models\WorldHeritage;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
9+
class Country extends Model
10+
{
11+
protected $table = 'countries';
12+
protected $connection = 'mysql';
13+
14+
protected $fillable = [
15+
'state_party_code',
16+
'name_en',
17+
'name_jp',
18+
'region'
19+
];
20+
21+
protected $primaryKey = 'state_party_code';
22+
public $incrementing = false;
23+
protected $keyType = 'string';
24+
25+
public function worldHeritageSites(): BelongsToMany
26+
{
27+
return $this->belongsToMany(
28+
WorldHeritage::class,
29+
'site_state_parties',
30+
'state_party_code',
31+
'world_heritage_site_id',
32+
'state_party_code',
33+
'id'
34+
)->withPivot(['is_primary','inscription_year'])
35+
->withTimestamps();
36+
}
37+
}

src/app/Models/WorldHeritage.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace App\Models;
44

5+
use App\Models\Country;
56
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
68

79
class WorldHeritage extends Model
810
{
911
protected $table = 'world_heritage_sites';
1012
protected $connection = 'mysql';
1113

1214
protected $fillable = [
13-
'id',
1415
'unesco_id',
1516
'official_name',
1617
'name',
@@ -40,4 +41,17 @@ class WorldHeritage extends Model
4041
'latitude' => 'float',
4142
'longitude' => 'float',
4243
];
44+
45+
public function countries(): BelongsToMany
46+
{
47+
return $this->belongsToMany(
48+
Country::class,
49+
'site_state_parties',
50+
'world_heritage_site_id',
51+
'state_party_code',
52+
'id',
53+
'state_party_code'
54+
)->withPivot(['is_primary','inscription_year'])
55+
->withTimestamps();
56+
}
4357
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('countries', function (Blueprint $t) {
15+
$t->char('state_party_code', 2)->primary();
16+
$t->string('name_en');
17+
$t->string('name_jp')->nullable();
18+
$t->string('region')->nullable();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
//
28+
}
29+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('site_state_parties', function (Blueprint $table) {
12+
$table->char('state_party_code', 2);
13+
14+
$table->foreignId('world_heritage_site_id')
15+
->constrained('world_heritage_sites')
16+
->cascadeOnDelete();
17+
18+
$table->boolean('is_primary')->default(false);
19+
$table->smallInteger('inscription_year')->nullable();
20+
$table->timestamps();
21+
22+
$table->foreign('state_party_code')
23+
->references('state_party_code')
24+
->on('countries')
25+
->cascadeOnUpdate()
26+
->cascadeOnDelete();
27+
28+
$table->primary(['state_party_code', 'world_heritage_site_id']);
29+
30+
$table->index(['world_heritage_site_id', 'state_party_code']);
31+
});
32+
}
33+
34+
public function down(): void
35+
{
36+
Schema::dropIfExists('state_party_world_heritage_site');
37+
}
38+
};

0 commit comments

Comments
 (0)