78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?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;
|
||
});
|
||
}
|
||
}
|