Skip to content

Commit f8b555c

Browse files
committed
add relations
1 parent 28d1140 commit f8b555c

File tree

10 files changed

+151
-0
lines changed

10 files changed

+151
-0
lines changed

app/Models/Comment.php

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

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78

89
class 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
}

app/Models/Review.php

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

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78

89
class 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
}

app/Models/Snippet.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use 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

811
class 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
}

app/Models/SnippetTag.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

app/Models/Tag.php

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

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
78

89
class 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
}

app/Models/User.php

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

55
// use Illuminate\Contracts\Auth\MustVerifyEmail;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Foundation\Auth\User as Authenticatable;
89
use Illuminate\Notifications\Notifiable;
910
use 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
}

graphql/schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#import type.graphql
2+
#import user.graphql
3+
#import snippet.graphql

graphql/snippet.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extend type Query {
2+
3+
}

graphql/type.graphql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

graphql/user.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extend type Query {
2+
getUser(id: ID! @eq): User @find
3+
}

0 commit comments

Comments
 (0)