Thanos Paravantis
Thanos Paravantis

Reputation: 7693

Laravel strange database user update

I am building an authentication app and while I was testing the user description and thumbnail I came up with an unusual problem. I currently have 2 users registered. The one is called parat26 and the other user1. I logged in the the first user parat26 and set my description to "test". After that, I logged in to my second user user1 and updated the description to "test123456". The strange part is that it did not update the user1 description but parat26

Here is the code:

Controller:

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

And the 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

Upvotes: 1

Views: 103

Answers (1)

Laurence
Laurence

Reputation: 60038

You call

$user = $user->first()

where you should not do that. And I bet you that the "first" user in your database is Parat26

Change your code from this

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

to this

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

    }

Upvotes: 2

Related Questions