Files
back-meditation/app/Models/Media.php
T
2026-06-13 17:20:42 +03:30

95 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\HasRatings;
use App\Traits\HasComments;
use App\Traits\HasSaves;
use App\Traits\HasLikes;
class Media extends Model
{
use HasRatings, HasComments,HasSaves,HasLikes;
protected $fillable = [
'user_id',
'image_id',
'detail_image_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',
'url',
];
public function image()
{
return $this->belongsTo(Image::class);
}
// Image shown on the detail (show-by-id) screen; image() is the list thumbnail.
public function detailImage()
{
return $this->belongsTo(Image::class, 'detail_image_id');
}
public function categories()
{
return $this->belongsToMany(Category::class, 'category_media');
}
public function subCategories()
{
return $this->belongsToMany(SubCategory::class, 'media_sub_category');
}
public function tags()
{
return $this->belongsToMany(Tag::class, 'media_tag');
}
public function plays()
{
return $this->hasMany(MediaPlay::class);
}
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;
}
// is_saved / saved_count come from the HasSaves trait (saved_items table),
// so the /saves/* endpoints and the media list/show all agree.
}