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.
wx.sstbc.com/app/Models/CourseContentEvaluationAsk.php

51 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models;
class CourseContentEvaluationAsk extends SoftDeletesModel
{
protected $casts = [
'select_item' => 'json'
];
public function courseContent()
{
return $this->hasOne(CourseContent::class, 'id', 'course_content_id');
}
/**
* 判断是否为多维度题目
* @return bool
*/
public function isMultiDimension()
{
return $this->edit_input === 'multi_dimension';
}
/**
* 获取维度数据(访问器)
* 当题目为多维度题目时返回维度数据,否则返回空数组
* 注意访问器会覆盖cast所以需要手动处理JSON解码
* @return array
*/
public function getDimensionsAttribute($value)
{
if ($this->isMultiDimension() && !empty($value)) {
// 手动处理JSON解码
return is_string($value) ? json_decode($value, true) : $value;
}
return [];
}
/**
* 设置维度数据(修改器)
* 将数组转换为JSON字符串存储
* @param mixed $value
*/
public function setDimensionsAttribute($value)
{
$this->attributes['dimensions'] = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
}
}