KTV/app/Models/Song.php

58 lines
1.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;
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',
'unnamed_21',
'simplified',
'phonetic_abbr',
'pinyin_abbr',
];
public function artists(){
return $this->belongsToMany(Artist::class);
}
public function categories(){
return $this->belongsToMany(SongCategory::class);
}
public function language(){
return $this->belongsTo(SongLanguage::class, 'languageType', 'id');
}
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);// 拼音首字母
});
}
}