File tree Expand file tree Collapse file tree 10 files changed +151
-0
lines changed
Expand file tree Collapse file tree 10 files changed +151
-0
lines changed Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Factories \HasFactory ;
66use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
78
89class Comment extends Model
910{
@@ -16,4 +17,14 @@ class Comment extends Model
1617 "user_id " ,
1718 "snippet_id "
1819 ];
20+
21+ public function user (): BelongsTo
22+ {
23+ return $ this ->belongsTo (User::class, "user_id " );
24+ }
25+
26+ public function snippet (): BelongsTo
27+ {
28+ return $ this ->belongsTo (Snippet::class, "snippet_id " );
29+ }
1930}
Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Factories \HasFactory ;
66use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
78
89class Review extends Model
910{
@@ -17,4 +18,14 @@ class Review extends Model
1718 "user_id " ,
1819 "snippet_id "
1920 ];
21+
22+ public function user (): BelongsTo
23+ {
24+ return $ this ->belongsTo (User::class, "user_id " );
25+ }
26+
27+ public function snippet (): BelongsTo
28+ {
29+ return $ this ->belongsTo (Snippet::class, "snippet_id " );
30+ }
2031}
Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Factories \HasFactory ;
66use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
8+ use Illuminate \Database \Eloquent \Relations \HasMany ;
9+ use Illuminate \Database \Eloquent \Relations \HasManyThrough ;
710
811class Snippet extends Model
912{
@@ -18,4 +21,29 @@ class Snippet extends Model
1821 "language " ,
1922 "owner_id "
2023 ];
24+
25+ public function tags (): HasManyThrough
26+ {
27+ return $ this ->hasManyThrough (Tag::class, SnippetTag::class);
28+ }
29+
30+ public function owner (): BelongsTo
31+ {
32+ return $ this ->belongsTo (User::class, "owner_id " );
33+ }
34+
35+ public function collaborators (): HasMany
36+ {
37+ return $ this ->hasMany (User::class);
38+ }
39+
40+ public function reviews (): HasMany
41+ {
42+ return $ this ->hasMany (Review::class, "snippet_id " );
43+ }
44+
45+ public function comments (): HasMany
46+ {
47+ return $ this ->hasMany (Comment::class, "snippet_id " );
48+ }
2149}
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+
8+ class SnippetTag extends Model
9+ {
10+ use HasFactory;
11+
12+ protected $ fillable = [
13+ "tag_id " ,
14+ "snippet_id "
15+ ];
16+
17+ protected $ table = "snippet_tag " ;
18+ }
Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Factories \HasFactory ;
66use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Relations \HasManyThrough ;
78
89class Tag extends Model
910{
@@ -14,4 +15,9 @@ class Tag extends Model
1415 protected $ fillable = [
1516 "name "
1617 ];
18+
19+ public function snippets (): HasManyThrough
20+ {
21+ return $ this ->hasManyThrough (Snippet::class, SnippetTag::class);
22+ }
1723}
Original file line number Diff line number Diff line change 44
55// use Illuminate\Contracts\Auth\MustVerifyEmail;
66use Illuminate \Database \Eloquent \Factories \HasFactory ;
7+ use Illuminate \Database \Eloquent \Relations \HasMany ;
78use Illuminate \Foundation \Auth \User as Authenticatable ;
89use Illuminate \Notifications \Notifiable ;
910use Laravel \Sanctum \HasApiTokens ;
@@ -42,4 +43,19 @@ class User extends Authenticatable
4243 ];
4344
4445 protected $ table = "users " ;
46+
47+ public function snippets (): HasMany
48+ {
49+ return $ this ->hasMany (Snippet::class, "owner_id " );
50+ }
51+
52+ public function comments (): HasMany
53+ {
54+ return $ this ->hasMany (Comment::class, "user_id " );
55+ }
56+
57+ public function reviews (): HasMany
58+ {
59+ return $ this ->hasMany (Review::class, "user_id " );
60+ }
4561}
Original file line number Diff line number Diff line change 1+ #import type.graphql
2+ #import user.graphql
3+ #import snippet.graphql
Original file line number Diff line number Diff line change 1+ extend type Query {
2+
3+ }
Original file line number Diff line number Diff line change 1+ type User {
2+ id : ID !
3+ username : String !
4+ email : String !
5+ snippets : [Snippet ]! @hasMany
6+ created_at : String
7+ updated_at : String
8+ comments : [Comment ]! @hasMany
9+ reviews : [Review ]! @hasMany
10+ }
11+
12+ type Snippet {
13+ id : ID !
14+ title : String !
15+ description : String !
16+ code : String !
17+ language : String !
18+ tags : [Tag ]! @hasMany
19+ owner : User ! @belongsTo
20+ collaborators : [User ]! @hasMany
21+ reviews : [Review ]! @hasMany
22+ comments : [Comment ]! @hasMany
23+ created_at : String
24+ updated_at : String
25+ }
26+
27+ type Tag {
28+ id : ID !
29+ name : String !
30+ snippets : [Snippet ]! @hasMany
31+ created_at : String
32+ updated_at : String
33+ }
34+
35+ type Comment {
36+ id : ID !
37+ text : String !
38+ user : User ! @belongsTo
39+ snippet : Snippet ! @belongsTo
40+ created_at : String
41+ updated_at : String
42+ }
43+
44+ type Review {
45+ id : ID !
46+ rating : Int !
47+ text : String !
48+ user : User ! @belongsTo
49+ snippet : Snippet ! @belongsTo
50+ created_at : String
51+ updated_at : String
52+ }
Original file line number Diff line number Diff line change 1+ extend type Query {
2+ getUser (id : ID ! @eq ): User @find
3+ }
You can’t perform that action at this time.
0 commit comments