76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; // ← Add this
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
|
use App\Traits\HasRatings;
|
|
use App\Traits\HasComments;
|
|
use App\Traits\HasSaves;
|
|
use App\Traits\HasLikes;
|
|
|
|
class Music extends Model
|
|
{
|
|
use HasFactory, HasRatings, HasComments , HasSaves,HasLikes;
|
|
protected $table = 'music';
|
|
|
|
|
|
protected $fillable = [
|
|
'user_id', 'title', 'artist', 'file_path', 'type',
|
|
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
|
];
|
|
protected $casts = [
|
|
'duration' => 'integer',
|
|
'order' => 'integer',
|
|
'is_active' => 'boolean',
|
|
];
|
|
protected $attributes = [
|
|
'type' => 'public', // Default value
|
|
];
|
|
// Relation to user (optional)
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
public function playlist(): BelongsTo
|
|
{
|
|
return $this->belongsTo(MusicPlaylist::class, 'playlist_id');
|
|
}
|
|
public function tags(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Tag::class, 'music_tags');
|
|
}
|
|
|
|
protected $appends = ['url',
|
|
'image_url' ,
|
|
'average_rating',
|
|
'user_rating',
|
|
'comments_count' ,
|
|
'has_user_commented',
|
|
'user_comment',
|
|
'user_comment_id',
|
|
'has_user_rated' ,
|
|
'is_saved',
|
|
'saved_count',
|
|
'is_liked',
|
|
'likes_count'
|
|
];
|
|
|
|
public function getUrlAttribute()
|
|
{
|
|
return asset('storage/' . $this->file_path);
|
|
}
|
|
public function image()
|
|
{
|
|
return $this->belongsTo(Image::class, 'image_id');
|
|
}
|
|
public function getImageUrlAttribute()
|
|
{
|
|
return $this->image ? asset('storage/' . $this->image->path) : null;
|
|
}
|
|
}
|