【问题标题】:Laravel - How to make students not to score beyond the max markLaravel - 如何让学生的分数不超过最高分
【发布时间】: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' : '' }}>&nbsp;&nbsp;{{ $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


    【解决方案1】:

    这可以通过几种不同的方式实现;您可以使用验证来防止有人输入超过最大标记的值,或者如果他们确实输入了超过最大标记的值,那么您只需默默地将其减小回最大标记。

    我建议您采用验证方法,为此您需要使用自定义验证规则。

    您可以阅读有关如何创建的文档 (https://laravel.com/docs/5.8/validation#custom-validation-rules),但实现可能看起来像这样:

    // Inside a form request (or you could use inline validation in your controller
    'student_mark' => [new NotOverMaxMark($this->input('subject_category_id'))]
    

    那么规则本身可能看起来像这样:

    <?php
    
    namespace App\Rules;
    
    use App\SubjectCategory;
    use Illuminate\Contracts\Validation\Rule;
    
    class NotOverMaxMark implements Rule
    {
        private $subjectCategory;
    
        public function __construct($subjectCategoryId)
        {
            $this->subjectCategory = SubjectCategory::findOrFail($subjectCategoryId);
        }
    
        public function passes($attribute, $value)
        {
            return $value <= $this->subjectCategory->max_mark;
        }
    
        public function message()
        {
            return 'The :attribute must not be higher than the max mark.';
        }
    }
    

    以上是您如何执行此操作的示例,您可能需要根据需要调整导入或实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多