Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

Undefined variable $roles

what is the problem in this code

                            <div class="mb-4">
                                @foreach ( $roles as $role )
                                
                                <label for="role" :value="__('Register as:')" />
                
                                <label>
                                    <input type="radio" name="role" value="{{ $role->id == '2' }}" checked /> Student
                                </label>
                                <label class="ml-2">
                                    <input type="radio" name="role" value="{{ $role->id == '3' }}" /> Teacher
                                </label>
                                @error('Role')
                                <div class="text-red-500">
                                    <small>{{ $message }}</small>
                                </div>
                                @enderror  
                                @endforeach
                            </div>,,,
0 likes
6 replies
tykus's avatar

The problem is you have an Undefined variable $roles

How does this Blade template get its data?

tykus's avatar

@Michael Fayez I don't know your application - you're going to have to share with us how you come to render this Blade template!

Lara_Love's avatar
Level 6

you not defined it in controller

write like this in your controller

$roles = Role::all();
OR
$roles = Role::where('id',$id);
OR
 anything . you should define it in controller

Please or to participate in this conversation.