最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - If I want to access 'admindashboard' I get 'Undefined variable $attendances'. I've

programmeradmin0浏览0评论

Here is my Routes

Route::get('/admin/dashboard', function () {
    $attendances = Attendance::with('staff')->orderBy('time', 'desc')->get();
    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');

AdminController

The controller does not give errors so I think the mistake might come from the dashboard part of the codes.

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

Here is the blade view. dashboard.blade.php I do not get error from the controller so I think the mistake might come from the this part of the codes.Maybe I used to attendance wrongly.

<!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>

Here is my Routes

Route::get('/admin/dashboard', function () {
    $attendances = Attendance::with('staff')->orderBy('time', 'desc')->get();
    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');

AdminController

The controller does not give errors so I think the mistake might come from the dashboard part of the codes.

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

Here is the blade view. dashboard.blade.php I do not get error from the controller so I think the mistake might come from the this part of the codes.Maybe I used to attendance wrongly.

<!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>
Share Improve this question edited Jan 19 at 22:03 matiaslauriti 8,1224 gold badges36 silver badges47 bronze badges asked Jan 19 at 19:30 Anyabour PeterAnyabour Peter 397 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You have 3 routes pointing to the same url '/admin/dashboard'. Laravel will ideally pick the last one defined which in this case doesn't point to the AdminController. I would say to take off the first and last routes pointing to the same url and leave the

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论