【发布时间】:2020-02-12 06:18:16
【问题描述】:
我正在将 Laravel-5.8 用于 Web 应用程序项目。在项目中,我有这些表:
class SubjectCategory extends Model
{
protected $table = 'subject_categories';
protected $fillable = [
'name',
'parent_id',
'max_mark',
];
public function children()
{
return $this->hasMany('App\Models\SubjectCategory', 'parent_id');
}
public function exams()
{
return $this->hasMany('App\Models\Exam');
}
}
class Exam extends Model
{
protected $table = 'exams';
protected $fillable = [
'subject_category_id',
'student_id',
'student_mark',
'subject_name',
];
public function subjectcategory()
{
return $this->belongsTo('App\Models\SubjectCategory','subject_category_id');
}
}
SubjectCategory 是一个层次表。只有父母有 max_mark
这里是控制器
public function create()
{
$categories = SubjectCategory::with('children')->whereNull('parent_id')->get();
return view('exams.create')
->with('categories', $categories);
}
public function store(StoreExamRequest $request)
{
$exam = new Exam();
$exam->stubject_category_id = $request->stubject_category_id;
$exam->student_id = $student_id;
$exam->student_mark = $request->student_mark;
$exam->save();
return redirect()->route('exams.index');
}
查看刀片
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="card card-secondary">
<form method="POST" action="{{route('exams.store')}}">
@csrf
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Subject Category:<span style="color:red;">*</span></label>
<select id="subject_category" class="form-control" name="subject_category_id">
<option value="">Select Subject Category</option>
@foreach ($categories as $category)
<option disabled="disabled" value="{{ $category->id }}" {{ $category->id == old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option>
@if ($category->children)
@foreach ($category->children as $child)
<option value="{{ $child->id }}" {{ $child->id == old('category_id') ? 'selected' : '' }}> {{ $child->name }}</option>
@endforeach
@endif
@endforeach
</select>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Subject Name:<span style="color:red;">*</span></label>
<input type="text" name="subject_name" placeholder="Enter Subject Name here" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<label class="control-label"> Mark Obtained:</label>
<input type="number" name="student_mark" placeholder="Enter Mark Obtained here" class="form-control">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
<!-- /.card -->
</div>
<!--/.col (left) -->
</div>
主题是分类的。在 SubjectCategory 中,父主题有子主题作为子主题。只有父学科类别有最高可得分数(Max Mark)。
来自考试 (subject_category_id) 下拉列表包含来自 subject_categories 的所有子字段。我想要实现的是:
当从下拉列表中选择学科类别时,系统会转到考试表。它根据 student_id 和 subject_category_id 显示总 student_mark。
在每个科目中,学生的累计分数不能超过父subject_category中的max_mark。
当用户尝试在 student_mark 文本字段中输入数据时,应用程序会将文本字段中的值添加到学生聚合中。如果结果大于基于父max_mark的subject_categories(SubjectCategory)中的max_mark,则显示错误信息。
我如何做到这一点?
谢谢。
【问题讨论】:
标签: laravel