File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
Expand file tree Collapse file tree 3 files changed +74
-0
lines changed 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 Comment extends Model
9+ {
10+ use HasFactory;
11+
12+ /**
13+ * The user who wrote the comment
14+ *
15+ * @return App\Models\User
16+ */
17+ public function author ()
18+ {
19+ return $ this ->belongsTo ('App\Models\User ' , 'user_id ' );
20+ }
21+
22+ /**
23+ * The post the comment is associated with
24+ *
25+ * @return App\Models\Post
26+ */
27+ public function post ()
28+ {
29+ return $ this ->belongsTo ('App\Models\Posts ' , 'post_id ' );
30+ }
31+
32+ }
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 Post extends Model
9+ {
10+ use HasFactory;
11+
12+ /**
13+ * Get the comments made on the post
14+ *
15+ * @return array App\Models\Comment
16+ */
17+ public function comments ()
18+ {
19+ return $ this ->hasMany ('App\Models\Comments ' , 'post_id ' );
20+ }
21+
22+ /**
23+ * Get the user who created the post
24+ *
25+ * @return App\Models\User
26+ */
27+ public function author ()
28+ {
29+ return $ this ->belongsTo ('App\Models\User ' , 'user_id ' );
30+ }
31+ }
Original file line number Diff line number Diff line change @@ -40,4 +40,15 @@ class User extends Authenticatable
4040 protected $ casts = [
4141 'email_verified_at ' => 'datetime ' ,
4242 ];
43+
44+ public function posts ()
45+ {
46+ return $ this ->hasMany ('App\Models\Posts ' , 'user_id ' );
47+ }
48+
49+ public function comments ()
50+ {
51+ return $ this ->hasMany ('App\Models\Comments ' , 'user_id ' );
52+ }
53+
4354}
You can’t perform that action at this time.
0 commit comments