简体   繁体   English

Laravel PHP 8 刀片 -> 获取所有父表记录的列表,将它们列在 select 元素中并根据外文ID选择记录->名称

[英]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.

相关问题 Laravel 多记录表的限制结果 select 基于是否所有记录都满足特定条件 - Laravel limit result of multiple record table select based on if all records meet specific criteria 列出表php和mysqli中的所有记录 - list all records in table php and mysqli Laravel 刀片多选。 如何选择数据透视表中的记录? - Laravel blade multiple select. How to select records in the pivot table? 列出使用php的mysql中的记录,但是总共只出现一条记录? - List records in mysql using php but only one record appear in all? 如何在 Php 中将父子平表记录显示为嵌套列表? - How to display parent child flat table records as nested list in Php? 如何通过在下拉列表中选择并在表格中显示来获取所有项目名称 - how to fetch the all item name by selecting in dropdown list and display in table MySQL选择所有记录,并为每个选择单独的记录-在一个查询中 - MySQL selecting all records and for each select separate record - in one query 试图从表B中选择所有与表A中最近添加的记录同名的记录 - Trying to select all the records from table B that have the same name as most recently added record from table A 在PHP表格中选择5条最新记录,并在HTML中填写有序列表 - Select the 5 latest records in a table with PHP and fill an ordered list with that in HTML 无法列出PHP和MySQLi中表中的所有记录 - Unable to list all records from the table in PHP and MySQLi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM