craigboyd
4 months ago

Error in my view

Posted 4 months ago by craigboyd

Hi All,

I am completely new to PHP. I am trying to do a drop down box populated by database values. I am more/less following this pattern: https://laracasts.com/discuss/channels/general-discussion/select-dropdown-option-seleted-in-laravel But I am getting an error:

ErrorException (E_ERROR)
Trying to get property 'id' of non-object (View: /home/craig/projects/web/laravelTest01/resources/views/courses/create.blade.php)

Here is my controller code:

   public function create()
    {
        $coursetypes = CourseType::pluck('type_nm','id');
        return view('courses.create', compact('coursetypes'));
    }

Here is my relevant(?) view code:

          <div class="form-group">
            <select class="form-control" name="course_type_id">
              @foreach($coursetypes as $coursetype )
              <option value="{{$coursetype->id}}">{{$coursetype->type_nm}}</option>
              @endforeach
            </select>
          </div>

Any ideas about where I am going wrong? Or is there an easier / better approach than the one I am taking?

Thanks.

Please sign in or create an account to participate in this conversation.