I am still getting to grips with the Laravel framework but something that should be simple is turning out to be a troublesome issue for me.
I have created a view inside account/update.blade.php
<form action="{{ URL::route('account-update-post') }}" method="post">
<p><input type="text" name="twitter" placeholder="Twitter handle" />
</p>
<p><input type="text" name="facebook" placeholder="Facebook handle" />
<p><input type="text" name="website" placeholder="Website url" />
<p><input type="text" name="about" placeholder="About me" />
<p><a href="{{ URL::route('account-change-password') }}">Change password</a></p>
<button class="button-default ">Update</button></p>
</form>
I have within AccountController.php
public function postUpdate(){
$user = User::find(Auth::user()->id);
$twitter = Input::get('twitter');
$facebook = Input::get('facebook');
return Redirect::route('account-update')
->with('global', 'Your account details could not be changed.');
}
I have created a Route:
/* Account update (POST) */
Route::post('/account/update', array(
'as' => 'account-update-post',
'uses' => 'AccountController@postUpdate'
));
Inside of my Models file I have Update.php
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array('twitter', 'facebook', 'website', 'about';
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
And have setup my database with the relevant fields to accept this information. I do not need any validation on the fields but struggling as to why it isnt updating into the DB..
Your code is working exactly as expected.
Try this
public function postUpdate() {
# Update user in DB
$user = User::find(Auth::id());
$user->twitter = Input::get('twitter');
$user->facebook = Input::get('facebook');
$user-save();
return Redirect::route('account-update')
->withGlobal('Your account has been successfully updated');
}
Updating A Retrieved Model
To update a model, you may retrieve it, change an attribute, and use the save method:
$user = User::find(Auth::id());
$user->twitter = Input::get('twitter');
$user->facebook = Input::get('facebook');
$user->save();
Then add your route to redirect to some page. See if that works. You can visit Insert - Update - Delete Section from Laravel Docs
You forgot to close the brackets.
protected $fillable = array('twitter', 'facebook', 'website', 'about';
should be
protected $fillable = array('twitter', 'facebook', 'website', 'about');