我仍在掌握 Laravel 框架,但一些本应简单的事情对我来说却是一个麻烦的问题。
我在 account/update.blade.php
中创建了一个 View
<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>
我在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.');
}
我创建了一条路线:
/* Account update (POST) */
Route::post('/account/update', array(
'as' => 'account-update-post',
'uses' => 'AccountController@postUpdate'
));
在我的模型文件中,我有 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';
并使用相关字段设置我的数据库以接受此信息。我不需要对字段进行任何验证,但苦苦思索为什么它没有更新到数据库中..
最佳答案
Updating A Retrieved Model
要更新模型,您可以检索它、更改属性并使用保存方法:
$user = User::find(Auth::id());
$user->twitter = Input::get('twitter');
$user->facebook = Input::get('facebook');
$user->save();
然后添加您的路由以重定向到某个页面。看看是否有效。您可以访问Insert - Update - Delete Section来自 Laravel 文档
关于php - Laravel 帐户详细信息更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265343/