[英]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
So to break that title down... I have 2 tables -> bosses (parent table) & employees (child) bosses hasMany employees and employees hasOne boss所以打破这个标题......我有2个表->老板(父表)和员工(孩子)老板有许多员工和员工有一个老板
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.
如果我从列表中更改老板,它应该将元素的 id 更改为新的选择。
@foreach ($bosses as $boss)
<option value="{{ $boss->id }}" {{ $boss->id == ($employee->boss_id) ? 'selected' : '' }}>
{{ $boss->full_name }}
</option>
@endforeach
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.使用上面的代码,我得到了按名称列出的所有 $publishers,但它没有选择与 boss_id 外键关联的老板。
Thanks in advance!!!提前致谢!!!
You could use the helper method old() for this.您可以为此使用辅助方法old() 。
For example:例如:
<select name="boss_id>
@foreach ($bosses as $boss)
<option value="{{$boss->id}}" {{ old('boss_id', $boss->id)==($employee->boss_id) ? "selected" : '' }}>
More info: https://laravel.com/docs/9.x/validation#repopulating-forms更多信息: https://laravel.com/docs/9.x/validation#repopulating-forms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.