KTV/app/Models/Song.php
allen.yan d9924bf05b 加入 ⻆色權限控制
修正 'n' => 'ㄣ' 轉值問題
加入 歌手搜尋功能
DB 開 點播次數欄位
20250505
2025-05-05 11:22:40 +08:00

78 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Helpers\ChineseNameConverter;
use App\Helpers\ChineseStrokesConverter;
class Song extends Model
{
/** @use HasFactory<\Database\Factories\SongFactory> */
use HasFactory;
protected $fillable = [
'name',
'adddate',
'filename',
'language_type',
'db_change',
'vocal',
'situation',
'copyright01',
'copyright02',
'note01',
'note02',
'note03',
'note04',
'enable',
'simplified',
'phonetic_abbr',
'pinyin_abbr',
'strokes_abbr',
];
protected function casts(): array
{
return [
'vocal' => 'boolean',
'enable' => 'boolean',
'language_type' => \App\Enums\SongLanguageType::class,
'situation' => \App\Enums\SongSituation::class,
];
}
public function users(){
return $this->belongsToMany(User::class, 'user_song')->withTimestamps();
}
public function str_artists(){
return $this->artists->pluck('name')->implode(', ');
}
public function artists(){
return $this->belongsToMany(Artist::class);
}
public function str_categories(){
return $this->categories->pluck('name')->implode(', ');
}
public function categories(){
return $this->belongsToMany(SongCategory::class);
}
protected static function booted()
{
// 無論是 creating 或 updating都執行這段共用的邏輯
static::saving(function ($song) {
$simplified=ChineseNameConverter::convertToSimplified($song->name);// 繁體轉簡體
$song->simplified = $simplified;
$song->phonetic_abbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified);// 注音符號
$song->pinyin_abbr=ChineseNameConverter::getKTVPinyinAbbr($simplified);// 拼音首字母
$chars = preg_split('//u', $song->name, -1, PREG_SPLIT_NO_EMPTY);
$firstChar = $chars[0] ?? null;
$song->strokes_abbr=$firstChar ? ChineseStrokesConverter::getStrokes($firstChar) : null;
});
}
}