File tree Expand file tree Collapse file tree 7 files changed +106
-0
lines changed Expand file tree Collapse file tree 7 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class PostResource extends JsonApiResource
1414 protected array $ relationships = [
1515 'comments ' ,
1616 'user ' ,
17+ 'tags ' ,
1718 ];
1819
1920 /**
Original file line number Diff line number Diff line change 66use Illuminate \Database \Eloquent \Model ;
77use Illuminate \Database \Eloquent \Relations \BelongsTo ;
88use Illuminate \Database \Eloquent \Relations \HasMany ;
9+ use Illuminate \Database \Eloquent \Relations \MorphToMany ;
910
1011class 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Models ;
4+
5+ use Illuminate \Database \Eloquent \Relations \MorphPivot ;
6+
7+ class Taggable extends MorphPivot
8+ {
9+ //
10+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 55use App \Enums \TeamRole ;
66use App \Models \Comment ;
77use App \Models \Post ;
8+ use App \Models \Tag ;
89use App \Models \Team ;
910use App \Models \User ;
1011use 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}
You can’t perform that action at this time.
0 commit comments