【问题标题】:How do I record student grades in Laravel?如何在 Laravel 中记录学生成绩?
【发布时间】:2021-09-18 23:47:26
【问题描述】:

我有一个电子期刊,我已经制作了一个包含 10 个等级的表格和学期控制 并实现了将学生添加到数据库的功能,我的最后一个目标是实现评分功能 问题是我停了下来,不知道如何记录每个学生的成绩

电子期刊页面(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', [JournalController::class, '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)
    {
        //
    }
}

【问题讨论】:

    标签: php mysql laravel function laravel-blade


    【解决方案1】:

    根据您的问题,我发现很难准确理解您要达到的目标。是否要存储学生的成绩?

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

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

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

    1. 您似乎在Student 模型上存储了1 - 10 分,但这些分数可能每学期都在变化?我建议创建一个Semester 模型和一个Mark 模型,其中Mark 具有BelongsToSemester 的关系,以及BelongsToManyUser 的关系。我相信documentation 会很有帮助。
    2. 您正在使用JournalController 保存Student 模型。您使用此控制器而不是为 Student 创建单独的控制器(如 StudentController)有什么特别的原因吗?
    3. JournalControllerstore() 函数中,您正在为用户存储未经哈希处理的密码。密码应始终进行哈希处理。你可以使用 Laravel 中的 Hash facade
    4. store() 函数也不受保护。但也许您稍后会添加Authorization

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

    【讨论】:

    • Landheeryes,这是个好主意,然后我会有一个页面,您可以在其中选择一个学期并查看该学期的学生成绩,因此我需要制作一个单独的表格来存储学生姓名,以便当您添加学生时,他们会将其添加到所有学期页表中,并创建更多表来存储每个学期的成绩,对吗?
    • 您可以创建SemesterMark 模型。 Semester 模型包含该学期的数据。 Mark 模型存储 student_idsemester_id(因此您可以识别该学期属于学生的分数)。这样,您不必“硬编码”分数 1 到 10。Student 模型可以简单地包含学生信息(姓名、地址、电子邮件、密码等)。请也检查我的其他提示并查看文档。如果您在任何时候遇到困难,请考虑提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多