master
cody 3 months ago
parent 4a8a5f68c2
commit 985d4a50b1

@ -4,11 +4,47 @@ namespace App\Models;
class CourseContentEvaluationAsk extends SoftDeletesModel
{
protected $casts = ['select_item' => 'json'];
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;
}
}

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('course_content_evaluation_asks', function (Blueprint $table) {
$table->json('dimensions')->nullable()->comment('多维度题目的维度数据');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('course_content_evaluation_asks', function (Blueprint $table) {
$table->dropColumn('dimensions');
});
}
};
Loading…
Cancel
Save