Files
back-meditation/app/Models/Media.php
T
2026-05-24 23:21:00 +03:30

86 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\HasRatings;
use App\Traits\HasComments;
use App\Traits\HasSaves;
class Media extends Model
{
use HasRatings, HasComments,HasSaves;
protected $fillable = [
'user_id',
'image_id',
'category_id',
'title',
'caption',
'type',
'file_path',
'external_url',
'duration',
'visibility',
'is_premium'
];
protected $appends = [
'average_rating',
'user_rating',
'ratings_count',
'comments_count',
'has_user_commented',
'user_comment',
'user_comment_id',
'has_user_rated',
'is_saved',
'saved_count',
'is_liked',
'likes_count'
];
public function image()
{
return $this->belongsTo(Image::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class, 'media_tag');
}
public function notes()
{
return $this->morphMany(Note::class, 'noteable');
}
public function myNote()
{
return $this->morphOne(Note::class, 'noteable')->where('user_id', auth()->id());
}
public function savedBy()
{
return $this->belongsToMany(User::class, 'saved_media')
->withTimestamps();
}
public function getUrlAttribute()
{
if ($this->file_path)
return asset('storage/' . $this->file_path);
return $this->external_url;
}
public function getIsSavedAttribute()
{
if (!auth()->check()) return false;
return $this->savedBy()
->where('user_id', auth()->id())
->exists();
}
}