Laravel奇怪的数据库用户更新


Laravel strange database user update

我正在构建一个身份验证应用程序,当我测试用户描述和缩略图时,我遇到了一个不寻常的问题。我目前有2个用户注册。一个叫parat26,另一个叫user1。我登录了第一个用户parat26,并将我的描述设置为"test"。之后,我登录了我的第二个用户user1,并将描述更新为"test123456"。奇怪的是,它没有更新user1的描述,但parat26

代码如下:

控制器:

<?php
class Account extends Base {
public function getSettings()
{
    return View::make('template.account.settings');
}
public function postSettings()
{
    $v = [
        "old_pw"        => "required",
        "new_pw"        => "required|max:50|min:6",
        "new_pw_again"  => "required|same:new_pw"
    ];
    $validator = Validator::make(Input::all(),  $v);
    if ($validator->fails())
    {
    } else {
        $user = User::find(Auth::user()->id);
        $old_pw = Input::get('old_pw');
        $new_pw = Input::get('new_pw');
        if (Hash::check($old_pw, $user->getAuthPassword()) || $user->save())
        {
            $user->password = Hash::make($new_pw);
            if ($user->save()) { return Redirect::route('account-settings')->with('success', trans('lang.success.settings')); }
        } else {
            return Redirect::route('account-settings')->with('error', trans('lang.error.settings'));
        }
    }
    return Redirect::route('account-settings')->with('error', trans('lang.error.settings_generic'));
}
public function getCustomize()
{
    return View::make('template.account.customize');
}
public function postCustomize()
{
    $v = [
        "thumbnail" => "max:1000|url",
        "description" => "max:100",
    ];
    $validator = Validator::make(Input::all(), $v);
    if ($validator->fails())
    {
        return Redirect::route('account-customize')->withErrors($validator)->withInput();
    } else {
        $user = User::find(Auth::user()->id);
        $thumbnail = e(trim(Input::get('thumbnail')));
        $description = e(trim(Input::get('description')));
        if ($user->count())
        {
            $user = $user->first();
            $user->thumbnail = $thumbnail;
            $user->description = $description;
            if ($user->save())
            {
                return Redirect::route('account-customize')->with('success', trans('lang.success.customize'));
            }
        }
    }
    return Redirect::route('account-customize')->with('error', trans('lang.error.settings_generic'));
}
}

和view:

    @extends('layout.dashboard')
@section('title')
    {{ trans('lang.title.customize') }}
@stop
@section('content')
<div class="row">
    <form action="{{ URL::route('account-customize-post') }}" method="post">
        <div class="col-lg-6">
            <h4>About</h4>
        </div>
        <div class="col-lg-6">
            <h4>Details</h4>
            <div class="form-group">
                <label for="description">Description</label>
                <textarea style="resize: vertical;" class="form-control" name="description" id="description">{{{ Auth::user()->description }}}</textarea>
                @if ($errors->has('description'))<p class="text-danger">{{ $errors->first('description') }}</p>@endif
            </div>
            <div class="form-group">
                <label for="thumbnail">Thumbnail</label>
                <input class="form-control" type="text" name="thumbnail" id="thumbnail" value="{{{ Auth::user()->thumbnail }}}">
                @if ($errors->has('thumbnail'))<p class="text-danger">{{ $errors->first('thumbnail') }}</p>@endif
            </div>
        </div>
</div>
@stop
@section('footer')
        <input class="btn btn-primary" type="submit" name="submit" value="{{ trans('lang.btn.save') }}">
        {{ Form::token() }}
    </form>
@stop

您调用

$user = $user->first()

中不应该这样做。我敢打赌,数据库中的"第一个"用户是Parat26

从这个

修改你的代码
} else {
    $user = User::find(Auth::user()->id);
    $thumbnail = e(trim(Input::get('thumbnail')));
    $description = e(trim(Input::get('description')));
    if ($user->count())
    {
        $user = $user->first();
        $user->thumbnail = $thumbnail;
        $user->description = $description;
        if ($user->save())
        {
            return Redirect::route('account-customize')->with('success', trans('lang.success.customize'));
        }
    }
}

} else {
        $user = Auth::user();
        $thumbnail = e(trim(Input::get('thumbnail')));
        $description = e(trim(Input::get('description')));
        $user->thumbnail = $thumbnail;
        $user->description = $description;
        if ($user->save())
        {
            return Redirect::route('account-customize')->with('success', trans('lang.success.customize'));
        }
    }