Почему в раскрывающемся списке Self Referencing ничего не отображается - PullRequest
0 голосов
/ 07 января 2020

В моем Laravel -5.8 проекте у меня есть эта модель:

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');
  }
}

Контроллер

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

web. php

Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {

Route::delete('holiday_groups/destroy', 'HrHolidayGroupsController@massDestroy')->name('holiday_groups.massDestroy');
Route::resource('holiday_groups', 'HrHolidayGroupsController');

Route::delete('employees/destroy', 'HrEmployeesController@massDestroy')->name('employees.massDestroy');
Route::resource('employees', 'HrEmployeesController');
});

HrEmployee ссылается на себя через line_manager_id и генерирует выпадающий список, отображая 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>
   </form>

route list

Но я заметил, что ничего не отображается.

Как мне решить эту проблему?

Ответы [ 2 ]

0 голосов
/ 07 января 2020

Попробуйте изменить способ отношений на hasOne или hasMany.

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

Обновление:

$linemanagers=HrEmployee::with('linemanager')->get();
0 голосов
/ 07 января 2020

В вашей модели Добавить protected $table = 'hremployees';

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

  public function linemanager()
  {
      return $this->belongsTo('App\Models\Hr\HrEmployee');
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...