Laravel Foreign Keys Drop-down List

eloquent, laravel, laravel-4

Solution

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

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

Problem

I have 2 tables: - CUSTOMERS(id, full_name, company_id) - COMPANIES(id, 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` 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.

Original source