60 lines
1.0 KiB
PHP
60 lines
1.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
|
||
|
|
class PostComment extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected $appends = ['is_edited', 'created_ago'];
|
||
|
|
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'post_id',
|
||
|
|
'user_id',
|
||
|
|
'reply_id',
|
||
|
|
'comment',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public function replies()
|
||
|
|
{
|
||
|
|
return $this->hasMany(PostComment::class, 'reply_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public function user() {
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns user is commentable.
|
||
|
|
*/
|
||
|
|
public function getIsEditedAttribute()
|
||
|
|
{
|
||
|
|
return loggedInUser()->id === $this->user_id;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns model in days since created_at.
|
||
|
|
*/
|
||
|
|
public function getCreatedAgoAttribute() {
|
||
|
|
return $this->created_at->diffForHumans();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public function post()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Post::class);
|
||
|
|
}
|
||
|
|
}
|