选项值默认值

时间:2017-04-08 18:08:44

标签: php laravel

我认为这一点,

<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']));
    }
}

我怎样才能使选项选择值默认为用户在数据库中选择的样式?

1 个答案:

答案 0 :(得分: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更改为控制器返回的内容。)