You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel 9中按日期范围查询指定学生的考勤记录

在Laravel 9中实现指定学生考勤记录查询功能

需求说明:从attendance表中查询指定学生在指定日期范围内的考勤记录,管理员通过表单输入班级、学号、起止日期提交查询。


1. 优化表单代码

原表单存在两处问题:roll number的name属性包含空格(后端接收会异常),多个input复用了同一个id。以下是修正后的表单:

<form class="mt-3" method="POST" action="/record">
    @csrf
    <div class="mb-3">
        <label for="studentClass" class="form-label">Student Class</label>
        <input type="number" name="class" class="form-control" id="studentClass" aria-describedby="emailHelp">
    </div>
    <div class="mb-3">
        <label for="rollNumber" class="form-label">Roll Number</label>
        <input type="number" name="roll_number" class="form-control" id="rollNumber">
    </div>
    <div class="mb-3">
        <label for="fromDate" class="form-label">From Date</label>
        <input type="date" name="from_date" class="form-control" id="fromDate">
    </div>
    <div class="mb-3">
        <label for="toDate" class="form-label">To Date</label>
        <input type="date" name="to_date" class="form-control" id="toDate">
    </div>
    <button type="submit" class="btn btn-primary">Generate</button>
</form>

2. 定义路由

在routes/web.php中添加POST路由,指向控制器的处理方法:

use App\Http\Controllers\AttendanceController;

Route::post('/record', [AttendanceController::class, 'getStudentAttendance'])->name('attendance.record');

3. 创建/配置Attendance模型

如果还未创建模型,执行Artisan命令生成:

php artisan make:model Attendance

在app/Models/Attendance.php中配置表关联与可填充字段(根据实际表结构调整):

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Attendance extends Model
{
    protected $fillable = ['class', 'roll_number', 'attendance_date', 'status']; // 按需添加其他字段
    protected $table = 'attendance'; // 若表名非复数形式,需手动指定
}

4. 编写控制器逻辑

创建AttendanceController(若不存在):

php artisan make:controller AttendanceController

在app/Http/Controllers/AttendanceController.php中实现查询逻辑:

namespace App\Http\Controllers;

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

class AttendanceController extends Controller
{
    public function getStudentAttendance(Request $request)
    {
        // 验证请求参数合法性
        $validated = $request->validate([
            'class' => 'required|integer',
            'roll_number' => 'required|integer',
            'from_date' => 'required|date',
            'to_date' => 'required|date|after_or_equal:from_date'
        ]);

        // 按条件查询考勤记录
        $attendanceRecords = Attendance::where('class', $validated['class'])
            ->where('roll_number', $validated['roll_number'])
            ->whereBetween('attendance_date', [$validated['from_date'], $validated['to_date']])
            ->orderBy('attendance_date', 'asc')
            ->get();

        // 返回结果视图并传递数据
        return view('attendance.results', compact('attendanceRecords', 'validated'));
    }
}

5. 创建结果展示视图

创建resources/views/attendance/results.blade.php用于展示查询结果:

<!DOCTYPE html>
<html>
<head>
    <title>Student Attendance Records</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container mt-5">
    <h2>Attendance Records for Class {{ $validated['class'] }} - Roll No. {{ $validated['roll_number'] }}</h2>
    <p>Date Range: {{ $validated['from_date'] }} to {{ $validated['to_date'] }}</p>

    @if($attendanceRecords->isEmpty())
        <div class="alert alert-info">该学生在指定日期范围内无考勤记录。</div>
    @else
        <table class="table table-striped mt-3">
            <thead>
                <tr>
                    <th>日期</th>
                    <th>考勤状态</th> <!-- 根据实际表结构调整列 -->
                    <!-- 可添加其他需要展示的字段 -->
                </tr>
            </thead>
            <tbody>
                @foreach($attendanceRecords as $record)
                <tr>
                    <td>{{ $record->attendance_date }}</td>
                    <td>{{ $record->status }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    <a href="{{ url()->previous() }}" class="btn btn-secondary mt-3">返回查询表单</a>
</body>
</html>

内容的提问来源于stack exchange,提问作者Ayaz Ahmad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:45:38