Skip to content

Commit 854e12f

Browse files
EduardEduard
Eduard
authored and
Eduard
committed
models relationships + er diagram
1 parent 13ce20c commit 854e12f

19 files changed

+655
-10
lines changed

Diff for: app/Models/City.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use App\Models\Relationships\CityRelationships;
67

78
class City extends Model
89
{
9-
//
10+
use CityRelationships;
1011
}

Diff for: app/Models/Country.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use App\Models\Relationships\CountryRelationships;
67

78
class Country extends Model
89
{
9-
//
10+
use CountryRelationships;
1011
}

Diff for: app/Models/Property.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use App\Models\Relationships\PropertyRelationships;
78

89
class Property extends Model
910
{
10-
use HasFactory;
11+
use HasFactory, PropertyRelationships;
1112
}

Diff for: app/Models/PropertyAddress.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use App\Models\Relationships\PropertyAddressRelationships;
78

89
class PropertyAddress extends Model
910
{
10-
use HasFactory;
11+
use HasFactory, PropertyAddressRelationships;
1112

1213
protected $table = 'property_address';
1314
}

Diff for: app/Models/PropertyStatus.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use App\Models\Relationships\PropertyStatusRelationships;
67

78
class PropertyStatus extends Model
89
{
9-
//
10+
use PropertyStatusRelationships;
1011
}

Diff for: app/Models/Relationships/CityRelationships.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use App\Models\Country;
7+
use App\Models\PropertyAddress;
8+
9+
trait CityRelationships
10+
{
11+
public function country(): BelongsTo
12+
{
13+
return $this->belongsTo(Country::class);
14+
}
15+
16+
public function propertyAddresses(): HasMany
17+
{
18+
return $this->hasMany(PropertyAddress::class);
19+
}
20+
}

Diff for: app/Models/Relationships/CountryRelationships.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use App\Models\City;
7+
8+
trait CountryRelationships
9+
{
10+
public function cities(): HasMany
11+
{
12+
return $this->hasMany(City::class);
13+
}
14+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use App\Models\Property;
7+
use App\Models\City;
8+
9+
trait PropertyAddressRelationships
10+
{
11+
public function property(): BelongsTo
12+
{
13+
return $this->belongsTo(Property::class, 'property_id', 'id');
14+
}
15+
16+
public function city(): BelongsTo
17+
{
18+
return $this->belongsTo(City::class, 'city_id', 'id');
19+
}
20+
}

Diff for: app/Models/Relationships/PropertyRelationships.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\Relations\HasOne;
7+
use App\Models\User;
8+
use App\Models\PropertyStatus;
9+
use App\Models\PropertyAddress;
10+
11+
trait PropertyRelationships
12+
{
13+
public function owner(): BelongsTo
14+
{
15+
return $this->belongsTo(User::class);
16+
}
17+
18+
public function status(): BelongsTo
19+
{
20+
return $this->belongsTo(PropertyStatus::class);
21+
}
22+
23+
public function address(): HasOne
24+
{
25+
return $this->hasOne(PropertyAddress::class);
26+
}
27+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use App\Models\Property;
7+
8+
trait PropertyStatusRelationships
9+
{
10+
public function properties(): HasMany
11+
{
12+
return $this->hasMany(Property::class);
13+
}
14+
}

Diff for: app/Models/Relationships/UserRelationships.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Models\Relationships;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use App\Models\Property;
7+
8+
trait UserRelationships
9+
{
10+
public function properties(): HasMany
11+
{
12+
return $this->hasMany(Property::class);
13+
}
14+
}

Diff for: app/Models/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use App\Models\Relationships\UserRelationships;
910

1011
class User extends Authenticatable
1112
{
1213
/** @use HasFactory<\Database\Factories\UserFactory> */
13-
use HasFactory, Notifiable;
14+
use HasFactory, Notifiable, UserRelationships;
1415

1516
/**
1617
* The attributes that are mass assignable.

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"require-dev": {
1717
"fakerphp/faker": "^1.23",
18+
"kevincobain2000/laravel-erd": "^1.7",
1819
"laravel/pail": "^1.1",
1920
"laravel/pint": "^1.13",
2021
"laravel/sail": "^1.39",

Diff for: composer.lock

+127-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: config/laravel-erd.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
return [
4+
'url' => 'erd',
5+
'middlewares' => [
6+
//Example
7+
//\App\Http\Middleware\NotFoundWhenProduction::class,
8+
],
9+
'models_path' => base_path('app/Models'),
10+
'docs_path' => base_path('docs/erd/'),
11+
12+
"display" => [
13+
"show_data_type" => false,
14+
15+
/**
16+
* Controls how the lines between models are drawn.
17+
*
18+
* Valid values are: Normal, Orthogonal, or AvoidsNodes.
19+
*
20+
* AvoidsNodes can be very slow in larger diagrams!
21+
*/
22+
"routing" => 'AvoidsNodes',
23+
],
24+
25+
"from_text" => [
26+
"BelongsTo" => "1..1\nBelongs To",
27+
"BelongsToMany" => "1..*\nBelongs To Many",
28+
"HasMany" => "1..*\nHas Many",
29+
"HasOne" => "1..1\nHas One",
30+
"ManyToMany" => "*..*\nMany To Many",
31+
"ManyToOne" => "*..1\nMany To One",
32+
"OneToMany" => "1..*\nOne To Many",
33+
"OneToOne" => "1..1\nOne To One",
34+
"MorphTo" => "1..1\n",
35+
"MorphToMany" => "1..*\n",
36+
],
37+
"to_text" => [
38+
"BelongsTo" => "",
39+
"BelongsToMany" => "",
40+
"HasMany" => "",
41+
"HasOne" => "",
42+
"ManyToMany" => "",
43+
"ManyToOne" => "",
44+
"OneToMany" => "",
45+
"OneToOne" => "",
46+
"MorphTo" => "",
47+
"MorphToMany" => "",
48+
],
49+
];

0 commit comments

Comments
 (0)