Skip to content

Commit 8fbb984

Browse files
committed
feat(article): update models to have respective relationships
1 parent 6bae7b1 commit 8fbb984

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

app/Models/Article.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1112
use Illuminate\Database\Eloquent\Relations\HasMany;
1213

1314
/**
@@ -81,4 +82,28 @@ public function comments(): HasMany
8182
{
8283
return $this->hasMany(Comment::class);
8384
}
85+
86+
/**
87+
* @return BelongsToMany<Category,Article>
88+
*/
89+
public function categories(): BelongsToMany
90+
{
91+
return $this->belongsToMany(Category::class, 'article_categories');
92+
}
93+
94+
/**
95+
* @return BelongsToMany<Tag,Article>
96+
*/
97+
public function tags(): BelongsToMany
98+
{
99+
return $this->belongsToMany(Tag::class, 'article_tags');
100+
}
101+
102+
/**
103+
* @return BelongsToMany<User,Article>
104+
*/
105+
public function authors(): BelongsToMany
106+
{
107+
return $this->belongsToMany(User::class, 'article_authors')->withPivot('role');
108+
}
84109
}

app/Models/Category.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
9-
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1010

1111
/**
1212
* @property int $id
@@ -34,10 +34,10 @@ protected function casts(): array
3434
}
3535

3636
/**
37-
* @return HasMany<ArticleCategory,Category>
37+
* @return BelongsToMany<Article,Category>
3838
*/
39-
public function articles(): HasMany
39+
public function articles(): BelongsToMany
4040
{
41-
return $this->hasMany(ArticleCategory::class);
41+
return $this->belongsToMany(Article::class, 'article_categories');
4242
}
4343
}

app/Models/Tag.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
910

1011
/**
1112
* @property int $id
@@ -33,4 +34,12 @@ protected function casts(): array
3334
{
3435
return [];
3536
}
37+
38+
/**
39+
* @return BelongsToMany<Article,Tag>
40+
*/
41+
public function articles(): BelongsToMany
42+
{
43+
return $this->belongsToMany(Article::class, 'article_tags');
44+
}
3645
}

0 commit comments

Comments
 (0)