Laravel PHP 8 blade -> getting a list of all parent table records, list them in a select element and selecting the record->name based on the foreignID

Viewed 43

So to break that title down... I have 2 tables -> bosses (parent table) & employees (child) bosses hasMany employees and employees hasOne boss

What I need to accomplish is to list all the bosses in an element and "select" the current boss. And if I change the boss from the list, it should change the id of the element to the new selection.

@foreach ($bosses as $boss) 
  <option value="{{ $boss->id }}" {{ $boss->id == ($employee->boss_id) ? 'selected' : '' }}>
    {{ $boss->full_name }}
  </option>
@endforeach

Controller

public function details($id){
    $employee = Employee::find($id);
    $bosses = Boss::get();
    return view('employees.details',[
        'employee' => $employee,
        'bosses' => $bosses,
    ] );
}

Right now I'm still setting everything up so the search isn't working.

With the above code I'm getting all of the $publishers listed by name but it is not selecting the the boss associated the the boss_id foreign key.

Thanks in advance!!!

1 Answers
Related