Files

52 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = [
'user_id',
'commentable_id',
'commentable_type',
'content',
];
protected $with = ['user'];
public function user()
{
return $this->belongsTo(User::class);
}
public function commentable()
{
return $this->morphTo();
}
// For backward compatibility with Media
public function media()
{
return $this->belongsTo(Media::class, 'commentable_id')->where('commentable_type', Media::class);
}
// For Music
public function music()
{
return $this->belongsTo(Music::class, 'commentable_id')->where('commentable_type', Music::class);
}
// Check if comment belongs to current user
public function getIsOwnerAttribute()
{
return auth()->check() && $this->user_id === auth()->id();
}
// Add timestamps formatted
public function getFormattedCreatedAtAttribute()
{
return $this->created_at->diffForHumans();
}
protected $appends = ['is_owner', 'formatted_created_at'];
}