2025-04-25 09:33:28 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use App\Helpers\ChineseNameConverter;
|
2025-04-30 16:10:47 +08:00
|
|
|
|
use App\Helpers\ChineseStrokesConverter;
|
2025-05-12 14:13:56 +08:00
|
|
|
|
use App\Traits\LogsModelActivity;
|
2025-04-25 09:33:28 +08:00
|
|
|
|
|
|
|
|
|
class Artist extends Model
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\ArtistFactory> */
|
2025-05-12 14:13:56 +08:00
|
|
|
|
use HasFactory, LogsModelActivity;
|
2025-04-25 09:33:28 +08:00
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
2025-04-29 14:18:07 +08:00
|
|
|
|
'category',
|
2025-04-25 09:33:28 +08:00
|
|
|
|
'name',
|
|
|
|
|
'simplified',
|
|
|
|
|
'phonetic_abbr',
|
|
|
|
|
'pinyin_abbr',
|
2025-04-30 16:10:47 +08:00
|
|
|
|
'strokes_abbr',
|
2025-05-06 13:04:32 +08:00
|
|
|
|
'enable',
|
2025-04-25 09:33:28 +08:00
|
|
|
|
];
|
|
|
|
|
|
2025-04-30 16:10:47 +08:00
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'category' => \App\Enums\ArtistCategory::class,
|
|
|
|
|
];
|
|
|
|
|
}
|
2025-05-12 14:13:56 +08:00
|
|
|
|
|
2025-04-25 09:33:28 +08:00
|
|
|
|
public function songs() {
|
|
|
|
|
return $this->belongsToMany(Song::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
// 無論是 creating 或 updating,都執行這段共用的邏輯
|
|
|
|
|
static::saving(function ($artist) {
|
|
|
|
|
$simplified=ChineseNameConverter::convertToSimplified($artist->name);// 繁體轉簡體
|
|
|
|
|
$artist->simplified = $simplified;
|
|
|
|
|
$artist->phonetic_abbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified);// 注音符號
|
|
|
|
|
$artist->pinyin_abbr=ChineseNameConverter::getKTVPinyinAbbr($simplified);// 拼音首字母
|
2025-04-30 16:10:47 +08:00
|
|
|
|
|
|
|
|
|
$chars = preg_split('//u', $artist->name, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
$firstChar = $chars[0] ?? null;
|
2025-05-10 09:00:14 +08:00
|
|
|
|
$artist->strokes_abbr=( $firstChar && preg_match('/\p{Han}/u', $firstChar) ) ? ChineseStrokesConverter::getStrokes($firstChar) : 0;
|
2025-04-25 09:33:28 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|