Skip to content
Advertisement

Laravel – Undefined variable: Instructor

I am trying to pass users with a specific role_id through a form on a create.blade.php see below;

@extends('layouts.app')

@section('content')
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Course</div>
                <div class="card-body">

                        <form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
                            @csrf

                            <div class="panel-body">
                                    @if (Auth::user()->isAdmin())
                                    <div class="row">
                                        <div class="col-xs-12 form-group">
                                            {!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
                                            {!! Form::select('Instructor[]', $Instructor, old('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
                                            <p class="help-block"></p>
                                            @if($errors->has('Instructor'))
                                                <p class="help-block">
                                                    {{ $errors->first('Instructor') }}
                                                </p>
                                            @endif
                                        </div>
                            </div>
                                    @endif

                            <div class="form-group">
                                <label class="required" for="name">Course Title</label>
                                <input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
                                @if($errors->has('name'))
                                <div class="invalid-feedback">
                                    {{ $errors->first('name') }}
                                </div>
                            @endif
                         </div>

                        <div class="form-group">
                            <button class="btn btn-danger" type="submit">
                                Save
                            </button>
                        </div>
                    </form>
                </div>
                </div>
@endsection

User.php

class User extends Authenticatable
{

protected $fillable = [
        'name', 'email', 'password',
    ];

protected $hidden = [
        'password', 'remember_token',
    ];

public function role()
    {
        return $this->belongsToMany(Role::class, 'role_user');
    }


    public function isAdmin()
    {
        return $this->role()->where('role_id', 1)->first();
    }

    public function roles(){


           return $this->belongsToMany('AppRole'); 
}

CoursesController.php

My create function:

protected function create()
    {
        {
            $Instructor = AppUser::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');

            // $courses = Course::all()->pluck('title');

            return view('admin.courses.create', compact('courses'));
        }
    }

The following error is being returned:

Undefined variable: Instructor (View: C:xampphtdocstestresourcesviewsadmincoursescreate.blade.php)

I am not sure where I have gone wrong. Thanks for any help.

Referring to my latest comment:

Referring to my latest comment

Advertisement

Answer

In the controller, you missing pass Instructor to your view:

return view('admin.courses.create', compact('courses', 'Instructor'));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement