Skip to content

Commit 9b36804

Browse files
committed
adding post, comment and adjusting user model
1 parent cc70e05 commit 9b36804

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

app/Models/Comment.php

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

app/Models/Post.php

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

app/Models/User.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)