You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB

<?php
namespace App\Models;
class AppointmentType extends SoftDeletesModel
{
protected $fillable = [
'admin_id', 'department_id', 'name', 'image_id', 'introduce',
'start_time', 'end_time', 'is_book', 'is_show', 'sort', 'content',
'tips', 'total', 'floor',
];
protected $casts = ['image_id' => 'json'];
protected $appends = ['image'];
public function getImageAttribute($value)
{
$ids = self::normalizeJsonIdList($this->image_id);
if (empty($ids)) {
return [];
}
return Upload::whereIn('id', $ids)->get();
}
/** 兼容 image_id 为单值、数组或脏数据的情况 */
public static function normalizeJsonIdList($value): array
{
if ($value === null || $value === '' || $value === []) {
return [];
}
if (is_array($value)) {
return array_values(array_filter($value, function ($id) {
return $id !== null && $id !== '' && is_numeric($id);
}));
}
if (is_numeric($value)) {
return [(int) $value];
}
return [];
}
public function image()
{
return $this->hasOne(Upload::class, 'id', 'image_id');
}
}