如果我想访问“管理/仪表板”,我会得到“未定义的变量$出勤”。尝试了很多方法还是不行

问题描述 投票:0回答:1

这是我的路线

Route::get('/admin/dashboard', function () {
    $attendances = Attendance::with('staff')->orderBy('time', 'desc')->get();attendance records
    return view('admin.dashboard', compact('attendances')); 
})->name('admin.dashboard');


Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/admin', [AdminController::class, 'dashboard'])->name('admin.dashboard');
});

Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');


Route::get('/admin/dashboard', function () {
    return view('admin.dashboard');
})->name('admin.dashboard');

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

管理控制器。控制器没有给出错误,所以我认为错误可能来自代码的仪表板部分。

public function dashboard()
{
    $attendances = Attendance::with('staff')->orderBy('time', 'desc')->get();
    return view('admin.dashboard', compact('attendances'));
}

这是刀片视图。仪表板.blade.php 我没有从控制器收到错误,所以我认为错误可能来自这部分代码。也许我以前出席错误。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard</title>
    @livewireStyles <!-- Required for Livewire -->
</head>
<body>
    <h1>Admin Dashboard</h1>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Location</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($attendances as $attendance)// The error points at this line
            <tr>
                <td>{{ $attendance->staff->name }}</td>
                <td>{{ ucfirst($attendance->type) }}</td>
                <td>{{ $attendance->location }}</td>
                <td>{{ $attendance->time }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

    <!-- Embed the StaffManager Livewire Component -->
    @livewire('staff-manager')

    @livewireScripts <!-- Required for Livewire -->
</body>
</html>
php laravel
1个回答
0
投票

您有 3 个路由指向相同的 url '/admin/dashboard'。 Laravel 理想情况下会选择最后一个定义的,在本例中它不指向 AdminController。我会说去掉指向同一网址的第一条和最后一条路线并保留

Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
© www.soinside.com 2019 - 2024. All rights reserved.