80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
|
|
class GuessOption extends Model
|
|
{
|
|
use HasFactory, HasDateTimeFormatter;
|
|
|
|
protected $table = 'guess_option';
|
|
|
|
protected $fillable = ['guess_id', 'image', 'option_name', 'status', 'correct_state', 'description'];
|
|
|
|
protected $attributes = [
|
|
'status' => self::STATUS_ABLE,
|
|
'correct_state' => self::UN_SET,
|
|
];
|
|
|
|
const STATUS_ABLE = 1; // 禁用
|
|
const STATUS_DISABLE = 0; // 启用
|
|
|
|
const CORRECT = 1; // 正确结果
|
|
const UN_CORRECT = 0; // 错误结果
|
|
const UN_SET = 2; //未设置
|
|
|
|
protected static function booted()
|
|
{
|
|
static::deleting(function ($guessOption) {
|
|
if ($guessOption->users()->exists()) {
|
|
throw new \Exception("已经有人竞猜无法删除");
|
|
}
|
|
$guessOption->users()->detach();
|
|
});
|
|
}
|
|
|
|
public static $correctStateMap = [
|
|
self::UN_SET => '结果统计中',
|
|
self::CORRECT => '竞猜正确',
|
|
self::UN_CORRECT => '竞猜失败',
|
|
// self::UN_SET => '未公布结果',
|
|
// self::CORRECT => '正确结果',
|
|
// self::UN_CORRECT => '错误结果',
|
|
];
|
|
|
|
public static $correctIcoMap = [
|
|
self::UN_SET => 'danger',
|
|
self::CORRECT => 'success',
|
|
self::UN_CORRECT => 'warning',
|
|
];
|
|
|
|
public static $statusMap = [
|
|
self::STATUS_DISABLE => '禁用',
|
|
self::STATUS_ABLE => '启用',
|
|
];
|
|
|
|
public function guess()
|
|
{
|
|
return $this->belongsTo(Guess::class, 'guess_id');
|
|
}
|
|
|
|
/**
|
|
* 选择此项的答题记录
|
|
*/
|
|
public function users()
|
|
{
|
|
return $this->belongsToMany(UserGuess::class, 'user_guess_option', 'guess_option_id', 'user_guess_id');
|
|
}
|
|
|
|
public function getStatusNameAttribute() {
|
|
return data_get(self::$statusMap, $this->status);
|
|
}
|
|
|
|
public function getCorrectStateNameAttribute() {
|
|
return data_get(self::$correctStateMap, $this->correct_state);
|
|
}
|
|
}
|