Skip to content

Commit 65a6f3d

Browse files
committed
Add polymorphic relationship
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 9971d5d commit 65a6f3d

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed

app/Http/Resources/PostResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class PostResource extends JsonApiResource
1414
protected array $relationships = [
1515
'comments',
1616
'user',
17+
'tags',
1718
];
1819

1920
/**

app/Models/Post.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
88
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
910

1011
class Post extends Model
1112
{
@@ -27,4 +28,12 @@ public function user(): BelongsTo
2728
{
2829
return $this->belongsTo(User::class);
2930
}
31+
32+
/**
33+
* Get all of the tags for the post.
34+
*/
35+
public function tags(): MorphToMany
36+
{
37+
return $this->morphToMany(Tag::class, 'taggable')->withPivot('notes')->using(Taggable::class);
38+
}
3039
}

app/Models/Tag.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8+
9+
class Tag extends Model
10+
{
11+
/** @use HasFactory<\Database\Factories\TagFactory> */
12+
use HasFactory;
13+
14+
/**
15+
* Get all of the posts that are assigned this tag.
16+
*/
17+
public function posts(): MorphToMany
18+
{
19+
return $this->morphedByMany(Post::class, 'taggable')->withPivot('notes')->using(Taggable::class);
20+
}
21+
}

app/Models/Taggable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Relations\MorphPivot;
6+
7+
class Taggable extends MorphPivot
8+
{
9+
//
10+
}

database/factories/TagFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Tag>
9+
*/
10+
class TagFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => $this->faker->word(),
21+
];
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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('tags', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
$table->timestamps();
18+
});
19+
20+
Schema::create('taggables', function (Blueprint $table) {
21+
$table->foreignId('tag_id');
22+
$table->foreignId('taggable_id');
23+
$table->string('taggable_type');
24+
$table->string('notes')->nullable();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('tags');
34+
Schema::dropIfExists('taggables');
35+
}
36+
};

database/seeders/DatabaseSeeder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Enums\TeamRole;
66
use App\Models\Comment;
77
use App\Models\Post;
8+
use App\Models\Tag;
89
use App\Models\Team;
910
use App\Models\User;
1011
use App\Models\UserProfile;
@@ -46,5 +47,10 @@ public function run(): void
4647
'post_id' => $posts->first()->getKey(),
4748
'user_id' => $user->getKey(),
4849
]);
50+
51+
$openSourceTag = Tag::factory()->create(['name' => 'Open Source']);
52+
$forgeTag = Tag::factory()->create(['name' => 'Forge']);
53+
54+
$posts->first()->tags()->attach([$openSourceTag]);
4955
}
5056
}

0 commit comments

Comments
 (0)