Laravel外键下拉列表 [英] Laravel Foreign Keys Drop-down List

查看:88
本文介绍了Laravel外键下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子:

  • CUSTOMERS(id,full_name,company_id)
  • COMPANIES(id,company_name)

我已经创建了两个表之间的关系,并且可以正常工作,因为我可以在客户视图中显示公司名称,如下所示:$customer->company->company_name

I already created the relation between the two tables, and it's working fine because I can display company name in the customers view like this: $customer->company->company_name

我现在遇到客户createedit视图的问题.我想在创建和编辑视图中将company_name作为下拉列表(表单选择").然后将公司ID插入CUSTOMERS表中.

I'm now having issues with the customer create and edit views. I'd like to have the company_name as a drop-down (Form Select) in create and edit views. Then insert the company id to the CUSTOMERS table.

推荐答案

您需要向公司提供Form :: select数组('id'=>'name'):

You need to supply Form::select with companies as an array('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表上提供的company_id并保存了客户.

After submitting the form validate the input, check if provided company_id exists on the companies table and save the customer, that's all.

这篇关于Laravel外键下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆