1 month ago

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 = [

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


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("") }}" method="post" class="form-horizontal" enctype="multipart/form-data">

          <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>
            <!-- /.form-group -->

      <!-- /.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>

But I observed that its displaying nothing.

How do I resolve this?

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