我有 2 张桌子:
- 客户(id、全名、公司 ID)
- 公司(id,公司名称)
我已经创建了两个表之间的关系,并且工作正常,因为我可以在客户视图中显示公司名称,如下所示:$customer->company->company_name
我现在与客户之间存在问题create
and edit
意见。我希望在创建和编辑视图中将 company_name 作为下拉列表(表单选择)。然后将公司 ID 插入到 CUSTOMERS 表中。
您需要以数组形式提供 Form::select 和公司('id'=>'name'):
// Controller, repo or wherever you want it:
$companies = Company::lists('company_name','id');
// $companies passed to the view, then in the create view:
{{ Form::select('company_id', $companies, null, $options) }}
// edit view:
{{ Form::model($customer, array('route' => array('YourCustomerUpdateRoute', $customer->id))) }}
...
{{ Form::select('company_id', $companies, null, $options) }}
// form model binding autopopulates the form, so correct option will be selected
提交表单后验证输入,检查公司表中是否存在提供的 company_id 并保存客户,仅此而已。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)