Laravel 5:每次编辑我的个人资料

时间:2015-10-09 16:51:09

标签: php laravel laravel-5 eloquent

每当我编辑我的个人资料,同时将password字段留空时,请将我的密码更改提交给其他哈希值。

如果我以纯文本格式向数据库提交密码并尝试编辑我的个人资料,同时将密码字段留空,则会删除我当前的密码并将其留空。

public function update(Request $request, User $user)
{
    $user = User::where('id', '=', Auth::id())->first();
    $user->name = Input::get('name');
    $user->email = Input::get('email');
    $user->password = Hash::make(Input::get('password'));
    $user->save();
}

{!! Form::model($user, ['method' => 'PATCH', 'action' => ['ProfilesController@update', $user->id]]) !!}
<fieldset>
    <div class="control-group">
        <!-- Username -->
        <label class="control-label"  for="name">Username</label>
        <div class="controls">
            <input type="text" id="name" name="name" placeholder="" value="{{ $user->name }}" class="form-control">
            <p class="help-block">Username can contain any letters or numbers, without spaces</p>
        </div>
    </div>

    <div class="control-group">
        <!-- E-mail -->
        <label class="control-label" for="email">E-mail</label>
        <div class="controls">
            <input type="text" id="email" name="email" placeholder="" value="{{ $user->email }}" class="form-control">
            <p class="help-block">Please provide your E-mail</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Password-->
        <label class="control-label" for="password">Password</label>
        <div class="controls">
            <input type="password" id="password" name="password" placeholder="" class="form-control">
            <p class="help-block">Password should be at least 4 characters</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Password -->
        <label class="control-label"  for="password_confirmation">Password (Confirm)</label>
        <div class="controls">
            <input type="password" id="password_confirmation" name="password_confirmation" placeholder="" class="form-control">
            <p class="help-block">Please confirm password</p>
        </div>
    </div>

    <div class="control-group">
        <!-- Button -->
        <div class="controls">
            <button class="btn btn-success">Register</button>
        </div>
    </div>
</fieldset>
{!! Form::close() !!}

1 个答案:

答案 0 :(得分:3)

您必须检查输入字段是否为空:

if(!empty(Input::get('password')) {
    $user->password = Hash::make(Input::get('password'));
}

有关哈希函数如何工作的更多信息,请查看此页面: https://mnshankar.wordpress.com/2014/03/29/laravel-hash-make-explained/