2017-04-08 218 views
1

我有我的看法這一點,選項值默認

<div class="form-group"> 
    <label for="theme" class="col-sm-3 control-label">Theme</label> 
    <select class="form-control" id="theme" name="theme"> 
     <option value="0">Default Theme</option> 
     <option value="1">Dark Theme</option> 
    </select> 
</div> 

,並在我的控制器

/** 
* Change User Account Settings 
* 
* @access public 
* @return view user.settings 
*/ 
public function changeSettings($username, $id) 
{ 
    $user = Auth::user(); 
    if (Request::isMethod('post')) { 
     $user->style = (int) Request::get('theme'); 
     $user->save(); 
    return Redirect::route('profil', ['username' => $user->username, 'id' => $user->id])->with(Toastr::success('Your Account Was Updated Successfully!', 'Yay!', ['options'])); 
    } else { 
     return redirect()->back()->with(Toastr::warning('Something Went Wrong!', 'Error', ['options'])); 
    } 
} 

我怎樣才能使它如此選擇值默認爲用戶選擇什麼風格的選項在數據庫中?

回答

0

在你看來,你做它像這樣:

<option value="0" @if($selectedTheme == 0) SELECTED @endif>Default Theme</option> 
<option value="1" @if($selectedTheme == 1) SELECTED @endif>Some other theme Theme</option> 

(當然,改變$selectedTheme到任何由你的控制器返回。)

+1

感謝完美地工作 –