Skip to content

Commit

Permalink
adding post, comment and adjusting user model
Browse files Browse the repository at this point in the history
  • Loading branch information
levijackson committed Jun 23, 2021
1 parent cc70e05 commit 9b36804
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
use HasFactory;

/**
* The user who wrote the comment
*
* @return App\Models\User
*/
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}

/**
* The post the comment is associated with
*
* @return App\Models\Post
*/
public function post()
{
return $this->belongsTo('App\Models\Posts', 'post_id');
}

}
31 changes: 31 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

/**
* Get the comments made on the post
*
* @return array App\Models\Comment
*/
public function comments()
{
return $this->hasMany('App\Models\Comments', 'post_id');
}

/**
* Get the user who created the post
*
* @return App\Models\User
*/
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function posts()
{
return $this->hasMany('App\Models\Posts', 'user_id');
}

public function comments()
{
return $this->hasMany('App\Models\Comments', 'user_id');
}

}

0 comments on commit 9b36804

Please sign in to comment.