Reputation: 541
I am trying to update the username column after a user updates his username. I am doing this with routers and filters and not using a controller.....
HTML:
Hi, <span class="username" contenteditable="true">{{ $username }} </span>
JS/jQuery
var username = $('.username').text();
$('.username').blur(function(){
if (username != $(this).text()) {
var username = $('.username').text();
console.log('Changing your name to ' + username);
$.ajax({
type: 'POST',
url: 'usernameChange',
data: { username: username }
}).done(function(msg){
console.log('Okay, new username is ' + msg);
});
}
});
It does send to the router successfully...
Route::post('usernameChange', function(){
$username = Input::get('username');
return $username;
});
But I am clueless where to update the table, or if I'm even doing it right.... I tried to append this into the usernameChange router, but I get a 505 internal error when trying to send the request...
DB::table('users')
->where('id', Auth::user()->id)
->update(array(Auth::user()->username => $username));
^ Is that even right?
So how can I do this and where should I?
Upvotes: 0
Views: 791
Reputation: 3622
Using Eloquent, you can do something like the following:
$user = Auth::user();
$user->username = $username;
$user->save();
I would recommend adding some validation into it before you save it. However, this is the basic idea. Load the user. Change what needs to be changed. Then call save on the user to update the information.
Upvotes: 1