phplaravel-livewirelaravel-routinglaravel-11livewire-3

Method App\Livewire\ListStudents::index does not exist


New to larvel was trying out a video tutorial...and i got stuck in this basic thing... not too sure what i am doing wrong ...

web.php

use App\Livewire\ListStudents;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;

Route::get('/', function () {
    return view('welcome');
});  
    
Route::middleware('auth')->group(function () {
    
    Route::get('/students',[ListStudents::class,'index'])->name('students');
});

require __DIR__.'/auth.php';

ListStudents.php

namespace App\Livewire;

use App\Models\Student;
use Livewire\Attributes\Layout;
use Livewire\Component;

class ListStudents extends Component
{

    #[Layout(name: "layouts.app")]
    public function render()
    {
        return view('livewire.list-students',data: [
            'students'=> Student::all(),
        ]);
    }
}

i have tried

Route::get('/students',[ListStudents::class,'index'])->name('students.index');
Route::get('/students',[ListStudents::class,'index']);

keeps giving me this ERROR:

BadMethodCallException - Method App\Livewire\ListStudents::index does not exist.

according to the original code in the video

Route::get('/students',[ListStudents::class])->name('students.index');

give me this error

ReflectionException - Function () does not exist

What am i doing wrong ? have i setup my project wrong? The authorization seems to work well (using a starter kit).

using Laravel 11 and livewire 3


Solution

  • You should set the route like this.

    Route::get('/students', ListStudents::class)->name('students.index');
    

    You're passing an array with a single element. Laravel expects either a callable or a class name, not an array.


    FYK: