I keep getting unidentified varible $data

Viewed 26

I'm trying to create roles and permissions through spatie on this laravel project I'm working on. So far i believe I've done most of the things right but i keep getting unidentified variable $data when i call the index blade and i can't seem to find nor fix the problem.

Here is my index.blade.php

<div class="card-body">
    <table class="table table-hover">
        <thead class="thead-dark">
            <tr>
                <th>#</th>
                <th>Name</th>
                <th width="280px">Action</th>
            </tr>
        </thead>
        <tbody>
            @php
            $x = 1;
            @endphp
            @foreach ($data as $role)
            <tr>
                <td>{{ $role->id }}</td>
                <td>{{ $role->name }}</td>
                <td>
                    <a class="btn btn-success" href="{{ route('roles.show',$role->id) }}">Show</a>
                    @can('role-edit')
                    <a class="btn btn-primary" href="{{ route('roles.edit',$role->id) }}">Edit</a>
                    @endcan
                    @can('role-delete')
                    {!! Form::open(['method' => 'DELETE','route' => ['roles.destroy', $role->id],'style'=>'display:inline']) !!}
                    {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                    {!! Form::close() !!}
                    @endcan
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    {{ $data->render() }}
</div>

This is my controller.

public function index(Request $request)
{
    $data = Role::orderBy('id', 'DESC')->paginate(5);
    // return 100;

    return view('roles.index', compact('data'));
    
}

This is my route.

Route::get('/roles/index', function () {
return view('roles.index');})->middleware(['auth'])->name('roles.index');}
1 Answers

You need to pass array containing controller class and action name as second parameter of Route::get() method

Route::get('/roles/index', [YourController::class, 'index'])
    ->middleware(['auth'])
    ->name('roles.index');;
Related