首页 > 解决方案 > 如何在 Laravel 中记录学生成绩?

问题描述

我有一个电子期刊,我已经做了一个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';

日记控制器.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)
    {
        //
    }
}

标签: phpmysqllaravelfunctionlaravel-blade

解决方案


从您的问题来看,我发现很难准确理解您要达到的目标。您要存储学生的成绩吗?

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

您是否尝试创建表单并调用控制器?有没有发生任何错误?

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

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

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


推荐阅读