noblemfd
1 month ago
174
6
Laravel

Self Referencing dropdown display nothing

Posted 1 month ago by noblemfd

In my Laravel-5.8 project, I have this model:

class HrEmployee extends Model
{
   protected $fillable = [
              'employee_code',
              'line_manager_id',
              'first_name',
              'last_name',
          ];

   public function linemanager()
   {
       return $this->belongsTo('App\Models\Hr\HrEmployee','line_manager_id');
   }
}

Controller

public function create()

    $linemanagers       =       HrEmployee::all();
    return view('hr.employees.create')
            ->with('linemanagers', $linemanagers);
}

HrEmployee is referencing itself through line_manager_id, and its to generate a dropdownlist by displaying first_name.

    <form action="{{ route("hr.employees.store") }}" method="post" class="form-horizontal" enctype="multipart/form-data">

        {{csrf_field()}}
          <div class="col-12 col-sm-4">
            <div class="form-group">
                <label class="control-label"> Line Manager:</label>
              <select class="form-control select2bs4" data-placeholder="Choose Line Manager" tabindex="1" name="line_manager_id" style="width: 100%;">
                <option value="">Select Line Manager</option>
                   @if($linemanagers->count() > 0 )
                     @foreach($linemanagers as $linemanager)
                       <option value="{{$linemanager->id}}">{{$linemanager->first_name}}</option>
                     @endforeach
                   @endif
              </select>
            </div>
            <!-- /.form-group -->
          </div>          
          

      </div>
      <!-- /.card-body -->
      <div class="card-footer">
        <button type="submit" class="btn btn-primary">Add Company</button>
        <button type="button" onclick="window.location.href='{{route('hr.employees.index')}}'" class="btn btn-default">Cancel</button>
      <!--</div>-->
    </div>
   </form>

But I observed that its displaying nothing.

How do I resolve this?

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