Tuesday, March 14, 2017

Laravel update the current authed user's settings

I'm new to laravel. I'm trying to change the currently logged in users settings.

Here's my code.

Controller:

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

public function postUpdateAccount(Request $request)
{
    $this->validate($request, [
        'username' => 'required|max:50|unique:users',
        'email' => 'required|email|unique:users',
        'first_name' => 'required|max:50',
        'last_name' => 'required|max:100',
        'password' => 'required|min:4'
    ]);
    $user = Auth::user();

    $user->username = $request['username'];
    $user->first_name = $request['first_name'];
    $user->last_name = $request['last_name'];
    $user->email = $request['email'];
    $user->password = bcrypt($request['password']);

    $user->update();
}

Blade:

<form action="" method="post">

<label for="username">Username:</label><input class="form-control" type="text" name="username" value="" placeholder="Username" autocomplete="off"><br>
<label for="first_name">First Name:</label><input class="form-control" type="text" name="first_name" value="" placeholder="First Name"><br>
<label for="last_name">Last Name:</label><input class="form-control" type="text" name="last_name" value="" placeholder="Last Name"><br>
<label for="email">Email Address:</label><input class="form-control" type="email" name="email" value="" placeholder="Email Address"><br>
<label for="password">New Password:</label><input class="form-control" type="password" name="password" placeholder="New Password" style="background-image:><br>
<small>You must enter your current password to save any settings. If you do not want to change your password then just add your current password to the "New Password" field.</small>
<br>
<label for="current_password">Current Password:</label><input class="form-control" type="password" name="current_password" placeholder="Current Password"  autocomplete="off"><br>
<button type="submit" class="btn btn-primary">Save Changed</button>

It seems to be getting stuck on the validate. I just used a return after the validate to check that.

Even with the validate not there it still does not save to the database.



via Jack Douglas

Advertisement