如何在 Laravel 中记录学生成绩?

问题描述

我有一个电子期刊,我已经在其中制作了一个包含 10 个成绩和学期控制的表格

this one

并实现了将学生添加数据库功能,我最后的目标是实现评分功能

this one

问题是我停了下来,不知道怎么给每个学生记成绩

电子期刊页面(journal.blade.PHP):

@extends('layouts.layout')
@section('title')Електронний журнал@endsection
@section ('content')
<div class="table-responsive">
    <table class="table table-dark">
      <thead>
        <tr>
          <th scope="col">ПІБ</th>
          <th scope="col">Тема 1</th>
          <th scope="col">Тема 2</th>
          <th scope="col">Тема 3</th>
          <th scope="col">Тема 4</th>
          <th scope="col">Тема 5</th>
          <th scope="col">Тема 6</th>
          <th scope="col">Тема 7</th>
          <th scope="col">Тема 8</th>
          <th scope="col">Тема 9</th>
          <th scope="col">Тема 10</th>
          <th scope="col">Семестровий контроль</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($students as $singleStudent)
        <tr>
            <td>{{ $singleStudent->name}}</td>
            <td>{{ $singleStudent->mark1}}</td>
            <td>{{ $singleStudent->mark2}}</td>
            <td>{{ $singleStudent->mark3}}</td>
            <td>{{ $singleStudent->mark4}}</td>
            <td>{{ $singleStudent->mark5}}</td>
            <td>{{ $singleStudent->mark6}}</td>
            <td>{{ $singleStudent->mark7}}</td>
            <td>{{ $singleStudent->mark8}}</td>
            <td>{{ $singleStudent->mark9}}</td>
            <td>{{ $singleStudent->mark10}}</td>
            <td>{{ $singleStudent->semester}}</td>
        </tr>
      @endforeach
    </tbody>
</table>
<a class="col-3 btn btn-outline-info" href="/createusers">Додати Учня</a>
<a class="ml-4 btn btn-outline-info" href="/mark">Оцінювання</a>
@endsection

将有评分系统的页面(mark.blade.PHP):

@extends('layouts.layout')
@section('title')Оцінювання@endsection
@section ('content')
<div class="row py-lg-5 ">
    <div class="col-lg-6 col-md-8 mx-auto">
        <h1 class="fw-light text-white">Оцінити учня</h1>
<form method="post" action="/journal">
{{ csrf_field() }}
<div class="login-form-1">
    <form id="login-form" class="text-left" novalidate="novalidate">
        <div class="login-form-main-message"></div>
        <div class="main-login-form">
            <div class="login-group">
                <div class="p-2">
                    <div class="form-group text-white">

                    </div>
                </div>
            </div>
            <div class="p-2">
    <button class="col-4 btn btn-outline-info mr-3" type="sumbit">Готово</button>
</div>
</form>
</div>
</div>
</div>
</section>
</p>
@endsection

路由文件(web.PHP):

Route::get('/',HomeController::class);

Route::resource('journal',JournalController::class);
Route::get('createusers',[JournalController::class,'create']);
Route::get('mark','mark']);

Route::get('/dashboard',function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

require __DIR__.'/auth.PHP';

JournalController.PHP:

<?PHP

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;

class JournalController extends Controller
{
    /**
     * display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $students = \App\Models\Student::all();
    return view('journal',compact('students'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('/createusers');
    }
    public function mark()
    {
        return view('/mark');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $students = new Student();
        $students->id = request('id');
        $students->name = request('name');
        $students->mark1 = request('mark1');
        $students->mark2 = request('mark2');
        $students->mark3 = request('mark3');
        $students->mark4 = request('mark4');
        $students->mark5 = request('mark5');
        $students->mark6 = request('mark6');
        $students->mark7 = request('mark7');
        $students->mark8 = request('mark8');
        $students->mark9 = request('mark9');
        $students->mark10 = request('mark10');
        $students->semester = request('semester');
        $students->password = request('password');
        $students->save();
        return redirect('/journal');
    }

    /**
     * display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $student = Student::query()->findOrFail($id);
        return view('journal',compact('student'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

解决方法

从您的问题中,我发现很难准确理解您想要实现的目标。您要存储学生的成绩吗?

似乎在您的 store()JournalController 函数中,您正在存储一个学生,并且您的分数为 1 - 10。您还存储了一个学期。

您是否尝试创建表单并调用控制器?有没有出错?

我有一些一般性评论可能对您的问题有所帮助:

  1. 您似乎在 Student 模型上存储分数 1 - 10,但这些分数可能每学期都在变化?我建议创建一个 Semester 模型和一个 Mark 模型,其中 MarkBelongsToSemester 关系,BelongsToMany 与 {{ 1}}。我相信 documentation 会很有帮助。
  2. 您正在使用 User 保存 JournalController 模型。您使用此控制器而不是为学生创建单独的控制器(例如 Student)有什么特别的原因吗?
  3. 在您的 StudentControllerstore() 函数中,您为用户存储了一个未散列的密码。密码应始终散列。您可以使用 Laravel 中可用的 Hash facade
  4. JournalController 函数也不受保护。但也许您稍后会添加 store()

改变你的问题可能会让人们更容易帮助你解决你的问题。