Home > Software design >  Laravel Passing Data into Blade giving Undefined
Laravel Passing Data into Blade giving Undefined

Time:04-11

When trying to pass data into my blade file I am getting an undefined variable error

Controller:

 public function index()
{
    $data = semester::all();

    return view("AdminViews/adminSemester", ['semesters'=>$data]);
}

Blade:

 @foreach ($semesters as $semester)
            <tr>
                
                <td>{{ $semester->name}}</td>
                <td>{{ $semester->code}}</td>
                <td>

It says $semesters is undefined even though i'm passing it correctly.

Also the route

Route::resource('semester', SemesterController::class);

CodePudding user response:

You forgot add your function index to the route. Don't forget declare full path to your controller in web.php (use App\Http\Controllers....) Please try change your route to the:

Route::resource('semester', [SemesterController::class, 'index']);

CodePudding user response:

Had an extra route pointed at /semester, Removed it and ran php artisan route:clear. All fixed

  • Related