Skip to content

Commit

Permalink
add relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
jericdei committed Oct 8, 2024
1 parent f01d542 commit 5104624
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Models/Barangay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Barangay extends Model
{
Expand All @@ -24,4 +25,19 @@ class Barangay extends Model
'name',
'old_name',
];

public function municipality(): BelongsTo
{
return $this->belongsTo(Municipality::class, 'municipality_code', 'municipality_code');
}

public function province(): BelongsTo
{
return $this->belongsTo(Province::class, 'province_code', 'province_code');
}

public function region(): BelongsTo
{
return $this->belongsTo(Region::class, 'region_code', 'region_code');
}
}
22 changes: 22 additions & 0 deletions src/Models/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class City extends Model
{
Expand All @@ -22,4 +24,24 @@ class City extends Model
'name',
'old_name',
];

public function province(): BelongsTo
{
return $this->belongsTo(Province::class, 'province_code', 'province_code');
}

public function region(): BelongsTo
{
return $this->belongsTo(Region::class, 'region_code', 'region_code');
}

public function barangays(): HasMany
{
return $this->hasMany(Barangay::class, 'city_code', 'municipality_code');
}

public function subMunicipalities(): HasMany
{
return $this->hasMany(SubMunicipality::class, 'province_code', 'province_code');
}
}
17 changes: 17 additions & 0 deletions src/Models/Municipality.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Municipality extends Model
{
Expand All @@ -22,4 +24,19 @@ class Municipality extends Model
'name',
'old_name',
];

public function province(): BelongsTo
{
return $this->belongsTo(Province::class, 'province_code', 'province_code');
}

public function region(): BelongsTo
{
return $this->belongsTo(Region::class, 'region_code', 'region_code');
}

public function barangays(): HasMany
{
return $this->hasMany(Barangay::class, 'municipality_code', 'municipality_code');
}
}
38 changes: 38 additions & 0 deletions src/Models/Province.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Jericdei\PsgcDatabase\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Province extends Model
{
Expand All @@ -21,4 +24,39 @@ class Province extends Model
'name',
'old_name',
];

public function region(): BelongsTo
{
return $this->belongsTo(Region::class, 'region_code', 'region_code');
}

public function barangays(): HasMany
{
return $this->hasMany(Barangay::class, 'province_code', 'province_code');
}

public function subMunicipalities(): HasMany
{
return $this->hasMany(SubMunicipality::class, 'province_code', 'province_code');
}

public function cities(): HasMany
{
return $this->hasMany(City::class, 'province_code', 'province_code');
}

public function municipalities(): HasMany
{
return $this->hasMany(Municipality::class, 'province_code', 'province_code');
}

/**
* Get merged collection of cities and municipalities.
*
* @return Collection<int, City|Municipality>
*/
public function getCitiesAndMunicipalities(): Collection
{
return $this->cities()->get()->merge($this->municipalities()->get());

Check failure on line 60 in src/Models/Province.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Jericdei\PsgcDatabase\Models\Province::getCitiesAndMunicipalities() should return Illuminate\Database\Eloquent\Collection<int, Jericdei\PsgcDatabase\Models\City|Jericdei\PsgcDatabase\Models\Municipality> but returns Illuminate\Database\Eloquent\Collection<int, Jericdei\PsgcDatabase\Models\City>.

Check failure on line 60 in src/Models/Province.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of method Illuminate\Database\Eloquent\Collection<int,Jericdei\PsgcDatabase\Models\City>::merge() expects iterable<(int|string), Jericdei\PsgcDatabase\Models\City>, Illuminate\Database\Eloquent\Collection<int, Jericdei\PsgcDatabase\Models\Municipality> given.
}
}
26 changes: 26 additions & 0 deletions src/Models/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Region extends Model
{
Expand All @@ -20,4 +21,29 @@ class Region extends Model
'name',
'old_name',
];

public function provinces(): HasMany
{
return $this->hasMany(Province::class, 'region_code', 'region_code');
}

public function cities(): HasMany
{
return $this->hasMany(City::class, 'region_code', 'region_code');
}

public function municipalities(): HasMany
{
return $this->hasMany(Municipality::class, 'region_code', 'region_code');
}

public function subMunicipalities(): HasMany
{
return $this->hasMany(SubMunicipality::class, 'region_code', 'region_code');
}

public function barangays(): HasMany
{
return $this->hasMany(Barangay::class, 'region_code', 'region_code');
}
}
11 changes: 11 additions & 0 deletions src/Models/SubMunicipality.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SubMunicipality extends Model
{
Expand All @@ -23,4 +24,14 @@ class SubMunicipality extends Model
'name',
'old_name',
];

public function city(): BelongsTo
{
return $this->belongsTo(City::class, 'province_code', 'province_code');
}

public function region(): BelongsTo
{
return $this->belongsTo(Region::class, 'region_code', 'region_code');
}
}

0 comments on commit 5104624

Please sign in to comment.