-1

I'm getting this error:

Undefined variable $nilais

This is the code from my main.blade.php

<tbody>
            @foreach ($nilais as $nilai)
            <tr>
                <td class="text-center" scope="row">{{ $nilai->NIM }}</td>
                <td>{{ $nilai->nama }}</td>
                <td class="text-center">{{ $nilai->presensi }}</td>
                <td class="text-center">{{ $nilai->keaktifan }}</td>
                <td class="text-center">{{ $nilai->tugas }}</td>
                <td class="text-center">{{ $nilai->UTS }}</td>
                <td class="text-center">{{ $nilai->UAS }}</td>
                <td class="text-center">{{ $nilai->total }}</td>
                <td class="text-center">Lulus</td>
                <td class="text-center">
                    <button type="button" class="btn btn-warning"> Edit </button>
                    <button type="button" class="btn btn-danger"> Hapus </button>
                </td>
            </tr>
            @endforeach

my Controller

class MainController extends Controller
{
    public function index(){
        $nilais = Nilai::get();
        return view('main', compact('nilais'));
    }}

and this is the route

Route::get('/main', function () {
    return view('main');
});

I don't know what is wrong, please help.

I'm aware there are other questions like this, but I already tried the solutions, and nothings work.

Thank you so much.

xodh
  • 11
  • 4

1 Answers1

3

Your MainController is never hit because you are returning view response from the route.

Change your route to

//import the FCQN for the controller via use statement
//use App\Http\Controllers\MainController;

Route::get('/main', [MainController::class, 'index']);

//OR without use statement
Route::get('/main', ['App\Http\Controllers\MainController', 'index']);
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • Thank you for answering. But after I change that, I got this error: Target class [MainController] does not exist. – xodh Jun 14 '22 at 00:45
  • @xodh A simple google search yielded: https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8. You need to add: `use App\Http\Controllers\MainController;` to the top of your routes.php file – Blue Jun 14 '22 at 00:47
  • You need to add the FCQN for the **MainController** via use statement in the routes file. Eg: `use App\Http\Controllers\MainController;` or provide FCQN string to the route definition `Route::get('/main', ['App\Http\Controllers\MainController', 'index']);` – Donkarnash Jun 14 '22 at 00:47