Laravel奇怪的数据库用户更新

时间:2014-05-17 11:17:52

标签: php laravel laravel-4

我正在构建一个身份验证应用程序,当我测试用户描述和缩略图时,我想出了一个不寻常的问题。我目前有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'));
}
}

观点:

    @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

1 个答案:

答案 0 :(得分:2)

你打电话

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

    }