KTV/app/Models/Song.php
allen.yan c1125ae141 加入完成訊息
song 滙入大量歌曲會有問題 修正
加入 歌曲字數
加入 檔案完成後刪除功能
加入 暫存檔案刪除功能
20250507
2025-05-07 15:35:06 +08:00

85 lines
2.5 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',
'song_number',
'song_counts',
];
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);
}
public function branches(){
return $this->belongsToMany(Branch::class)
->withPivot('counts')
->withTimestamps();
}
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;
$song->song_number = mb_strlen($song->name, 'UTF-8');
});
}
}