2014-01-23 54 views
0

我有一個用戶表,它引用了公司表中的一個company_id。我已經成功地填充在編輯用戶視圖公司下拉列表中,但是當我改變公司並單擊保存,它不會更新用戶表項以反映新的公司:如何從Laravel 4的下拉列表中更新數據庫條目

UserController.php

public function edit($id) 
{ 
    $user = $this->user->find($id); 

    // build a select list of companies 
    $options = DB::table('companies')->orderBy('name', 'asc')->lists('name', 'id'); 

    // get the existing company for the user 
    $selected = Input::old('company') ? Input::old('company') : $user->company->id; 

    if (is_null($user)) 
    { 
     return Redirect::route('users.index'); 
    } 

    return View::make('users.edit', array('options' => $options, 'selected' => $selected), compact('user')); 
} 

並在視圖我有這樣的下拉列表:

{{ Form::label('company', 'Company:') }} 
    {{ Form::select('company', $options, $selected) }} 

我懷疑我的$選擇的變量被莫名其妙的干擾,所以這將是告訴更新方法忽略之前的$選擇的價值,如果一個最好的方式在下拉菜單中選擇了新的價值?

回答

0

我這樣做太複雜了。 $選擇的變量是不必要的,因爲我在表列參考中有一個錯字。以下是工作原理:

{{ Form::label('company_id', 'Company:') }} 
{{ Form::select('company_id', $options) }} 

正如你所看到的,當我符合我的選擇ID(「COMPANY_ID」而不是「公司」)爲在我的模型,Laravel鏈接描述和預期更新它們的領域。

相關問題